Restore marker-only cleanup in the Word ribbon
This commit is contained in:
parent
1bd082bea4
commit
710f1775e0
@ -1 +1 @@
|
||||
<Project><PropertyGroup><ApplicationVersion>1.0.0.18</ApplicationVersion></PropertyGroup></Project>
|
||||
<Project><PropertyGroup><ApplicationVersion>1.0.0.19</ApplicationVersion></PropertyGroup></Project>
|
||||
|
||||
@ -6,4 +6,11 @@ public static class DocumentMaintenancePolicy
|
||||
public static bool OwnsControl(string? tag){var value=(tag??"").Trim();return value.StartsWith("PD-CHAPTER-",System.StringComparison.OrdinalIgnoreCase)||value.StartsWith("PD-SCENE-",System.StringComparison.OrdinalIgnoreCase);}
|
||||
public static bool OwnsBookmark(string? name)=>name!=null&&(name.StartsWith("_PD_CHAPTER_",System.StringComparison.Ordinal)||name.StartsWith("_PD_SCENE_",System.StringComparison.Ordinal));
|
||||
public static readonly string[] Properties={"PlotDirector.Native.Association","PlotDirector.Native.DocumentGuid","PlotDirector.Native.ProjectID","PlotDirector.Native.BookID","PlotDirector.Native.ServerUrl"};
|
||||
public static bool IsLegacyControl(string? tag)=>HasNumericSuffix(tag,"PD-CHAPTER-")||HasNumericSuffix(tag,"PD-SCENE-");
|
||||
public static bool IsLegacyBookmark(string? name)=>HasNumericSuffix(name,"_PD_CHAPTER_")||HasNumericSuffix(name,"_PD_SCENE_");
|
||||
|
||||
private static bool HasNumericSuffix(string? value,string prefix)
|
||||
=> value!=null && value.StartsWith(prefix,System.StringComparison.OrdinalIgnoreCase)
|
||||
&& int.TryParse(value.Substring(prefix.Length),System.Globalization.NumberStyles.None,System.Globalization.CultureInfo.InvariantCulture,out var id)
|
||||
&& id>0;
|
||||
}
|
||||
|
||||
@ -174,6 +174,7 @@ Check(!CompanionPresentation.NeedsAuthorisation(new(){State="Reconnecting",Authe
|
||||
Check(CompanionPresentation.NeedsAuthorisation(new(){State="Action required"}),"invalid installation offers the existing browser sign-in action");
|
||||
Check(CompanionPresentation.WordStatus(false,3,DateTime.UtcNow,"ok",DateTime.UtcNow).Contains("not connected"),"server success never implies Word is connected");
|
||||
Check(CompanionPresentation.WordStatus(true,3,DateTime.UtcNow.AddMinutes(-2),"ok",DateTime.UtcNow).Contains("pending"),"stale Word registration cannot be displayed as connected to book");
|
||||
Check(DocumentMaintenancePolicy.IsLegacyControl("PD-CHAPTER-12")&&DocumentMaintenancePolicy.IsLegacyControl("PD-SCENE-42")&&DocumentMaintenancePolicy.IsLegacyBookmark("_PD_CHAPTER_12")&&DocumentMaintenancePolicy.IsLegacyBookmark("_PD_SCENE_42")&&!DocumentMaintenancePolicy.IsLegacyControl("PD-SCENE-notes")&&!DocumentMaintenancePolicy.IsLegacyBookmark("_PD_SCENE_42_notes")&&!DocumentMaintenancePolicy.IsLegacyBookmark("AuthorBookmark"),"ribbon cleanup matches exact legacy marker identifiers only");
|
||||
Check(DocumentMaintenancePolicy.OwnsControl(" pd-scene-12 ")&&DocumentMaintenancePolicy.OwnsBookmark("_PD_CHAPTER_9"),"maintenance recognises established PlotDirector marker ownership");
|
||||
Check(!DocumentMaintenancePolicy.OwnsControl("Author chapter")&&!DocumentMaintenancePolicy.OwnsControl("OTHER-PD-SCENE-1")&&!DocumentMaintenancePolicy.OwnsBookmark("AuthorBookmark"),"maintenance leaves unrelated controls and bookmarks alone");
|
||||
Check(DocumentMaintenancePolicy.Properties.Length==5&&!DocumentMaintenancePolicy.Properties.Contains("Author"),"document-property deletion uses an explicit owned allowlist");
|
||||
|
||||
@ -14,6 +14,7 @@ namespace PlotDirector.WordAddIn
|
||||
public void OnLoad(Office.IRibbonUI ribbonUI){ui=ribbonUI;}
|
||||
public void Invalidate(){try{ui?.Invalidate();}catch(COMException){}}
|
||||
public string GetStatus(Office.IRibbonControl control)=>"Local Companion: "+(adapter()?.ConnectionState??"unavailable");
|
||||
public void RemoveLegacyMarkers(Office.IRibbonControl control)=>adapter()?.RemoveLegacyMarkers();
|
||||
public void OpenCompanion(Office.IRibbonControl control)=>adapter()?.OpenCompanion();
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,6 @@ using System.Runtime.InteropServices;
|
||||
[assembly: AssemblyTitle("PlotDirector Word Companion — Native development")]
|
||||
[assembly: AssemblyCompany("Nick Beckley")]
|
||||
[assembly: AssemblyProduct("PlotDirector")]
|
||||
[assembly: AssemblyVersion("1.0.0.18")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.18")]
|
||||
[assembly: AssemblyVersion("1.0.0.19")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.19")]
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
@ -2,5 +2,6 @@
|
||||
<ribbon><tabs><tab id="PlotDirectorTab" label="PlotDirector"><group id="PlotDirectorConnection" label="Companion">
|
||||
<labelControl id="PlotDirectorStatus" getLabel="GetStatus" />
|
||||
<button id="PlotDirectorOpen" label="Open Companion" imageMso="FileOpen" onAction="OpenCompanion" supertip="Open the Windows tray Companion. The indicator reports local communication only." />
|
||||
<button id="PlotDirectorRemoveLegacyMarkers" label="Remove Legacy Markers" onAction="RemoveLegacyMarkers" supertip="Remove old PlotDirector chapter and scene anchors from this document while keeping the manuscript and its identity." />
|
||||
</group></tab></tabs></ribbon>
|
||||
</customUI>
|
||||
|
||||
@ -267,18 +267,46 @@ namespace PlotDirector.WordAddIn
|
||||
}))Reply(command,"Word queue busy. Retry from Companion.");
|
||||
}
|
||||
// Retain no collection/range RCWs between scheduler turns. One marker per turn; typing/save/close take priority.
|
||||
private void StartMaintenance(LocalDocument doc,AdapterCommand command)
|
||||
public void RemoveLegacyMarkers()
|
||||
{
|
||||
const string title="Remove Legacy Markers";
|
||||
try
|
||||
{
|
||||
var document=Com(()=>application.ActiveDocument);
|
||||
if(document==null||!documents.TryGetValue(document,out var doc)||closing.Contains(document))
|
||||
{MessageBox.Show("Open a writable manuscript and try again.",title);return;}
|
||||
if(doc.Maintenance!=null){MessageBox.Show("A document maintenance operation is already running.",title);return;}
|
||||
StartMaintenance(doc,new AdapterCommand{Kind="maintenance-markers",SessionId=doc.Session,Revision=doc.Revision},
|
||||
result=>MessageBox.Show(result,title));
|
||||
}
|
||||
catch(Exception ex)when(ex is COMException||ex is InvalidOperationException||ex is ArgumentException)
|
||||
{MessageBox.Show("Word could not start marker removal. Check that the document is open and writable.",title);}
|
||||
}
|
||||
private void StartMaintenance(LocalDocument doc,AdapterCommand command, Action<string> localResult=null)
|
||||
{
|
||||
var kind=command.Kind.Substring("maintenance-".Length);
|
||||
bool legacy=localResult!=null;
|
||||
if(!DocumentMaintenancePolicy.IsAction(kind))throw new ArgumentException("Unknown maintenance operation.");
|
||||
if(kind!="inspect"&&Com(()=>doc.Document.ReadOnly))throw new InvalidOperationException("Document is read-only.");
|
||||
CancelExtraction(doc);
|
||||
doc.Maintenance=command;var started=Stopwatch.StartNew();
|
||||
var stage=kind=="reset"?2:0;var index=kind=="reset"?0:-1;var count=0;var inspected=0;var removed=0;var report=new System.Text.StringBuilder();
|
||||
var stage=kind=="reset"?2:0;var index=kind=="reset"?0:-1;var count=0;var inspected=0;var removed=0;var found=0;var report=new System.Text.StringBuilder();
|
||||
void Finish(string result)
|
||||
{
|
||||
if(!legacy){Reply(command,result);return;}
|
||||
if(doc.Maintenance==command)doc.Maintenance=null;
|
||||
if(legacy)
|
||||
{
|
||||
var summary=result.StartsWith("Complete:",StringComparison.Ordinal)
|
||||
? found==0?"No legacy PlotDirector markers remain.":$"Found {found} legacy PlotDirector markers. Removed {removed}. Save this document to keep the cleanup."
|
||||
: $"{result}\r\nFound {found} markers so far. Removed {removed}.";
|
||||
localResult(summary);
|
||||
}
|
||||
}
|
||||
Action next=null;
|
||||
next=()=>{
|
||||
if(doc.Maintenance!=command)return;
|
||||
if(started.Elapsed>TimeSpan.FromMinutes(2)||!documents.ContainsKey(doc.Document)||closing.Contains(doc.Document)||doc.Revision!=command.Revision){Reply(command,"Stopped: document closed, saved or changed. Some markers may already be removed; inspect again.");return;}
|
||||
if(started.Elapsed>TimeSpan.FromMinutes(2)||!documents.ContainsKey(doc.Document)||closing.Contains(doc.Document)||doc.Revision!=command.Revision){Finish("Stopped: document closed, saved or changed. Some markers may already be removed; inspect again.");return;}
|
||||
try{
|
||||
if(++inspected>100000)throw new InvalidOperationException("Maintenance limit reached; inspect again.");
|
||||
if(stage==0){
|
||||
@ -286,28 +314,29 @@ namespace PlotDirector.WordAddIn
|
||||
try{controls=Com(()=>doc.Document.ContentControls);count=Com(()=>controls.Count);if(index<0)index=count;
|
||||
if(index>count)throw new InvalidOperationException("Marker collection changed; inspect again.");
|
||||
if(index==0){stage=1;index=-1;}
|
||||
else{marker=Com(()=>controls[index--]);var tag=Com(()=>marker.Tag);if(DocumentMaintenancePolicy.OwnsControl(tag)){if(report.Length<3500)report.AppendLine("Control: "+tag);if(kind is "markers" or "clean"){Com(()=>{marker.Delete(false);return true;});removed++;}}}
|
||||
else{marker=Com(()=>controls[index--]);var tag=Com(()=>marker.Tag);if(legacy?DocumentMaintenancePolicy.IsLegacyControl(tag):DocumentMaintenancePolicy.OwnsControl(tag)){found++;if(report.Length<3500)report.AppendLine("Control: "+tag);if(kind is "markers" or "clean"){if(legacy&&Com(()=>marker.LockContentControl)){Com(()=>{marker.LockContentControl=false;return true;});try{Com(()=>{marker.Delete(false);return true;});}catch{Com(()=>{marker.LockContentControl=true;return true;});throw;}}else Com(()=>{marker.Delete(false);return true;});removed++;}}}
|
||||
}finally{Release(marker);Release(controls);}
|
||||
}else if(stage==1){
|
||||
Word.Bookmarks bookmarks=null;Word.Bookmark marker=null;bool hidden=false;bool read=false;
|
||||
try{bookmarks=Com(()=>doc.Document.Bookmarks);hidden=Com(()=>bookmarks.ShowHidden);read=true;Com(()=>{bookmarks.ShowHidden=true;return true;});count=Com(()=>bookmarks.Count);if(index<0)index=count;
|
||||
if(index>count)throw new InvalidOperationException("Bookmarks changed; inspect again.");
|
||||
if(index==0){stage=2;index=0;}
|
||||
else{object key=index--;marker=Com(()=>bookmarks.get_Item(ref key));var name=Com(()=>marker.Name);if(DocumentMaintenancePolicy.OwnsBookmark(name)){if(report.Length<3500)report.AppendLine("Bookmark: "+name);if(kind is "markers" or "clean"){Com(()=>{marker.Delete();return true;});removed++;}}}
|
||||
if(index==0){stage=legacy?3:2;index=0;}
|
||||
else{object key=index--;marker=Com(()=>bookmarks.get_Item(ref key));var name=Com(()=>marker.Name);if(legacy?DocumentMaintenancePolicy.IsLegacyBookmark(name):DocumentMaintenancePolicy.OwnsBookmark(name)){found++;if(report.Length<3500)report.AppendLine("Bookmark: "+name);if(kind is "markers" or "clean"){Com(()=>{marker.Delete();return true;});removed++;}}}
|
||||
}finally{if(read)Com(()=>{bookmarks.ShowHidden=hidden;return true;});Release(marker);Release(bookmarks);}
|
||||
}else if(index<DocumentMaintenancePolicy.Properties.Length){
|
||||
}else if(!legacy&&index<DocumentMaintenancePolicy.Properties.Length){
|
||||
var name=DocumentMaintenancePolicy.Properties[index++];var value=ReadProperty(doc.Document,name);
|
||||
if(value!=""){if(report.Length<3500)report.AppendLine(name+": "+value.Substring(0,Math.Min(512,value.Length)));
|
||||
if(kind is "reset" or "clean"){object properties=null,property=null;try{properties=Com(()=>doc.Document.CustomDocumentProperties);dynamic collection=properties;property=Com<object>(()=>collection[name]);dynamic item=property;Com(()=>{item.Delete();return true;});}finally{Release(property);Release(properties);}}}
|
||||
}else{
|
||||
if(kind is "reset" or "clean"){// The live session keeps its identity until close; a new open allocates an identity when metadata is absent.
|
||||
doc.IdentityPersisted=false;doc.BookId=null;doc.ProjectId=null;doc.AssociationSite="";link.Register(Descriptor(doc));}
|
||||
Reply(command,"Complete: "+(kind=="inspect"?"PlotDirector identifiers (only owned metadata shown)":$"{removed} owned markers removed. Prose retained. Save Word normally if you wish to keep changes.")+"\r\n"+report);return;
|
||||
if(legacy&&removed>0)Com(()=>{doc.Document.Saved=false;return true;});
|
||||
Finish("Complete: "+(kind=="inspect"?"PlotDirector identifiers (only owned metadata shown)":$"{removed} owned markers removed. Prose retained. Save Word normally if you wish to keep changes.")+"\r\n"+report);return;
|
||||
}
|
||||
if(!scheduler.Post("maintenance:"+doc.Session,next))Reply(command,"Stopped: Word queue busy; inspect before retrying.");
|
||||
}catch(Exception e)when(e is COMException||e is InvalidOperationException||e is ArgumentException){Reply(command,"Stopped: Word maintenance could not complete ("+e.GetType().Name+"). Some metadata may already be removed; inspect before retrying. No prose was deliberately edited.");}
|
||||
if(!scheduler.Post("maintenance:"+doc.Session,next))Finish("Stopped: Word queue busy; inspect before retrying.");
|
||||
}catch(Exception e)when(e is COMException||e is InvalidOperationException||e is ArgumentException){Finish("Stopped: Word maintenance could not complete ("+e.GetType().Name+"). Some metadata may already be removed; inspect before retrying. No prose was deliberately edited.");}
|
||||
};
|
||||
if(!scheduler.Post("maintenance:"+doc.Session,next))Reply(command,"Stopped: Word queue busy.");
|
||||
if(!scheduler.Post("maintenance:"+doc.Session,next))Finish("Stopped: Word queue busy.");
|
||||
}
|
||||
private void Reply(AdapterCommand command,string result){var doc=documents.Values.FirstOrDefault(d=>d.Session==command.SessionId);if(doc?.Maintenance==command)doc.Maintenance=null;link.TryPost(new Message{Kind="adapter-result",SessionId=command.SessionId,Revision=command.Revision,SnapshotId=command.Id,OperationMessage=result});}
|
||||
private bool ScheduleSnapshot(LocalDocument doc,bool analyse=false,Guid? snapshot=null,bool paragraphs=false)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user