Present chapter synchronisation on the native Word ribbon
This commit is contained in:
parent
88c6116663
commit
025765e85f
@ -104,6 +104,7 @@ public sealed class SessionManager(CloudGateway? cloud = null,ChapterAiWork? ai
|
||||
if(message.Kind.StartsWith("ai-",StringComparison.Ordinal))return ai?.Handle(document.Descriptor,message,answer)??Reject(answer,"ai-unavailable");
|
||||
switch (message.Kind)
|
||||
{
|
||||
case "tray-context":case "tray-intent": break;
|
||||
case "tray-sync":case "tray-repair":if(!word.ChapterCapable)return Reject(answer,"unsupported-chapter-capability");break;
|
||||
case "metrics": break;
|
||||
case "adapter-result":break;
|
||||
|
||||
@ -39,6 +39,8 @@ public sealed partial class TrayCoordinator(SessionManager sessions,CloudGateway
|
||||
if(request.Kind=="selection"){doc.Cursor=request.Start;doc.WordSaved=request.WordSaved;if(request.WordSaved==true&&doc.AssociationNeedsSave){doc.AssociationNeedsSave=false;doc.AssociationNotice="";}}
|
||||
ChapterFrame(doc,request,answer);
|
||||
if(request.Kind is "tray-sync" or "tray-repair")_ = RequestChapterFromRibbon(doc,request.Revision);
|
||||
if ((request.Kind is "tray-context" or "tray-intent") && answer.Status == "ok")
|
||||
_ = OpenRibbonWorkspace(doc.Session, request.Revision, request.Kind == "tray-context" ? "context" : "intent");
|
||||
CaptureFrame(doc,request,answer);
|
||||
if((request.Kind=="selection"||request.Kind=="snapshot-end")&&!ChapterCapable(instance)){doc.ChapterId=answer.ChapterId;doc.SceneId=answer.SceneId;doc.ChapterTitle=answer.ChapterTitle;doc.SceneTitle=answer.SceneTitle;}
|
||||
if(answer.WordCount.HasValue)doc.Words=answer.WordCount;
|
||||
@ -120,6 +122,17 @@ public sealed partial class TrayCoordinator(SessionManager sessions,CloudGateway
|
||||
}}}
|
||||
}
|
||||
private static bool DocumentBindingRestore(TrayDocument doc,Message state)=>doc.RememberedBook>0&&state.Health?.Authentication=="verified"&&Uri.TryCreate(doc.AssociationSite,UriKind.Absolute,out var a)&&Uri.TryCreate(state.ServerUrl,UriKind.Absolute,out var b)&&a==b&&state.Books.Any(book=>book.BookId==doc.RememberedBook);
|
||||
private Task OpenRibbonWorkspace(Guid session, long revision, string page) => Task.Run(() =>
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
if (!documents.TryGetValue(session, out var doc) || doc.Revision != revision) return;
|
||||
try { OpenPage(session, page); }
|
||||
catch (Exception error) when (error is InvalidOperationException or IOException or System.ComponentModel.Win32Exception)
|
||||
{ doc.Status = error.Message; }
|
||||
}
|
||||
});
|
||||
|
||||
public void OpenPage(Guid session,string page){lock(gate){if(!documents.TryGetValue(session,out var doc)||!cloud.Allows(doc.Authentication,doc.Book))throw new InvalidOperationException("Select an authorised book first.");cloud.OpenCompanionPage(page=="review"&&!string.IsNullOrEmpty(doc.ReviewPath)?doc.ReviewPath:$"/native-companion/workspace?bookId={doc.Book}&instanceId={doc.Instance}&sessionId={doc.Session}&view={page}",doc.Authentication);}}
|
||||
public async Task Heartbeat(CancellationToken cancellation)
|
||||
{
|
||||
|
||||
@ -1,25 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using Office=Microsoft.Office.Core;
|
||||
using Office = Microsoft.Office.Core;
|
||||
|
||||
namespace PlotDirector.WordAddIn
|
||||
{
|
||||
[ComVisible(true)]
|
||||
public sealed class NativeRibbon:Office.IRibbonExtensibility
|
||||
public sealed class NativeRibbon : Office.IRibbonExtensibility
|
||||
{
|
||||
private readonly Func<WordAdapter> adapter;private Office.IRibbonUI ui;private string lastStatus,lastSync;
|
||||
public NativeRibbon(Func<WordAdapter> adapter){this.adapter=adapter;}
|
||||
public string GetCustomUI(string ribbonID){using(var reader=new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("PlotDirector.WordAddIn.Ribbon.xml")))return reader.ReadToEnd();}
|
||||
public void OnLoad(Office.IRibbonUI ribbonUI){ui=ribbonUI;lastStatus=GetStatus(null);lastSync=GetSyncStatus(null)+GetSyncTip(null);}
|
||||
public void Invalidate(){try{var status=GetStatus(null);if(status!=lastStatus){lastStatus=status;ui?.InvalidateControl("PlotDirectorStatus");}var sync=GetSyncStatus(null)+GetSyncTip(null);if(sync!=lastSync){lastSync=sync;ui?.InvalidateControl("PlotDirectorSyncStatus");}}catch(COMException){}}
|
||||
public string GetSyncStatus(Office.IRibbonControl control)=>adapter()?.ChapterStatusLabel??"Chapter index unavailable";
|
||||
public string GetSyncTip(Office.IRibbonControl control)=>adapter()?.ChapterStatusTip??"Open Companion to connect this manuscript.";
|
||||
public void Synchronise(Office.IRibbonControl control)=>adapter()?.RequestChapterSynchronisation(false);
|
||||
public void Repair(Office.IRibbonControl control)=>adapter()?.RequestChapterSynchronisation(true);
|
||||
public void Troubleshooting(Office.IRibbonControl control)=>adapter()?.ShowTroubleshooting();
|
||||
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();
|
||||
private readonly Func<WordAdapter> adapter;
|
||||
private readonly Dictionary<string, string> displayed = new Dictionary<string, string>();
|
||||
private Office.IRibbonUI ui;
|
||||
|
||||
public NativeRibbon(Func<WordAdapter> adapter) { this.adapter = adapter; }
|
||||
|
||||
public string GetCustomUI(string ribbonID)
|
||||
{
|
||||
using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("PlotDirector.WordAddIn.Ribbon.xml")))
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
public void OnLoad(Office.IRibbonUI ribbonUI)
|
||||
{
|
||||
ui = ribbonUI;
|
||||
displayed.Clear();
|
||||
}
|
||||
|
||||
public void Invalidate()
|
||||
{
|
||||
if (ui == null) return;
|
||||
try
|
||||
{
|
||||
Refresh("PlotDirectorStatus", GetStatus(null));
|
||||
Refresh("PlotDirectorBook", GetBookTip(null));
|
||||
Refresh("PlotDirectorLocation", GetLocationTip(null));
|
||||
Refresh("PlotDirectorSyncStatus", GetSyncStatus(null) + "\n" + GetSyncTip(null));
|
||||
Refresh("PlotDirectorSyncProgress", GetSyncProgress(null));
|
||||
var enabled = CanOpenBook(null).ToString();
|
||||
Refresh("PlotDirectorContext", enabled);
|
||||
Refresh("PlotDirectorIntent", enabled);
|
||||
Refresh("PlotDirectorSynchronise", enabled);
|
||||
Refresh("PlotDirectorRepair", enabled);
|
||||
}
|
||||
catch (COMException) { }
|
||||
}
|
||||
|
||||
private void Refresh(string id, string value)
|
||||
{
|
||||
if (displayed.TryGetValue(id, out var previous) && previous == value) return;
|
||||
ui.InvalidateControl(id);
|
||||
displayed[id] = value;
|
||||
}
|
||||
|
||||
private static string ShortLabel(string text, int limit) =>
|
||||
text.Length <= limit ? text : text.Substring(0, limit - (char.IsHighSurrogate(text[limit - 1]) ? 1 : 0)) + "…";
|
||||
|
||||
public bool CanOpenBook(Office.IRibbonControl control) => adapter()?.HasRibbonBook == true;
|
||||
public string GetBook(Office.IRibbonControl control) => ShortLabel(GetBookTip(control), 25);
|
||||
public string GetBookTip(Office.IRibbonControl control) => adapter()?.RibbonBook ?? "No linked book";
|
||||
public string GetLocation(Office.IRibbonControl control) => ShortLabel(GetLocationTip(control), 32);
|
||||
public string GetLocationTip(Office.IRibbonControl control) => adapter()?.RibbonLocation ?? "Location unavailable";
|
||||
public string GetSyncStatus(Office.IRibbonControl control) => adapter()?.ChapterStatusLabel ?? "Not connected";
|
||||
public string GetSyncProgress(Office.IRibbonControl control) => adapter()?.RibbonSyncProgress ?? " ";
|
||||
public string GetSyncTip(Office.IRibbonControl control) => adapter()?.ChapterStatusTip ?? "Open Companion to connect.";
|
||||
public string GetStatus(Office.IRibbonControl control) => adapter()?.ConnectionState == "connected" ? "Companion connected" : "Companion offline";
|
||||
public void OpenContext(Office.IRibbonControl control) => adapter()?.OpenWritingWorkspace("tray-context");
|
||||
public void OpenIntent(Office.IRibbonControl control) => adapter()?.OpenWritingWorkspace("tray-intent");
|
||||
public void Synchronise(Office.IRibbonControl control) => adapter()?.RequestChapterSynchronisation(false);
|
||||
public void Repair(Office.IRibbonControl control) => adapter()?.RequestChapterSynchronisation(true);
|
||||
public void Troubleshooting(Office.IRibbonControl control) => adapter()?.ShowTroubleshooting();
|
||||
public void RemoveLegacyMarkers(Office.IRibbonControl control) => adapter()?.RemoveLegacyMarkers();
|
||||
public void OpenCompanion(Office.IRibbonControl control) => adapter()?.OpenCompanion();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +1,30 @@
|
||||
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="OnLoad">
|
||||
<ribbon><tabs><tab id="PlotDirectorTab" label="PlotDirector"><group id="PlotDirectorConnection" label="Companion">
|
||||
<labelControl id="PlotDirectorStatus" getLabel="GetStatus" />
|
||||
<labelControl id="PlotDirectorSyncStatus" getLabel="GetSyncStatus" getSupertip="GetSyncTip" />
|
||||
<menu id="PlotDirectorManuscript" label="Manuscript">
|
||||
<button id="PlotDirectorSynchronise" label="Synchronise now" onAction="Synchronise" supertip="Check current chapter mappings using the separate Companion agent." />
|
||||
<button id="PlotDirectorRepair" label="Repair synchronisation" onAction="Repair" supertip="Validate or repair the existing import. Existing chapters, scenes and author decisions are preserved." />
|
||||
<button id="PlotDirectorTroubleshooting" label="Troubleshooting" onAction="Troubleshooting" />
|
||||
</menu>
|
||||
<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>
|
||||
<ribbon>
|
||||
<tabs>
|
||||
<tab id="PlotDirectorTab" label="PlotDirector">
|
||||
<group id="PlotDirectorManuscript" label="Manuscript">
|
||||
<labelControl id="PlotDirectorBook" getLabel="GetBook" getSupertip="GetBookTip" />
|
||||
<labelControl id="PlotDirectorLocation" getLabel="GetLocation" getSupertip="GetLocationTip" />
|
||||
</group>
|
||||
<group id="PlotDirectorWriting" label="Writing">
|
||||
<button id="PlotDirectorContext" label="Writing Context" size="large" imageMso="ReadingMode" onAction="OpenContext" getEnabled="CanOpenBook" />
|
||||
<button id="PlotDirectorIntent" label="Writing Intent" size="large" imageMso="ReviewNewComment" onAction="OpenIntent" getEnabled="CanOpenBook" />
|
||||
</group>
|
||||
<group id="PlotDirectorSync" label="Synchronisation">
|
||||
<button id="PlotDirectorSyncStatus" getLabel="GetSyncStatus" getSupertip="GetSyncTip" imageMso="FileDocumentInspect" onAction="Troubleshooting" />
|
||||
<labelControl id="PlotDirectorSyncProgress" getLabel="GetSyncProgress" />
|
||||
<button id="PlotDirectorSynchronise" label="Synchronise" imageMso="RefreshAll" onAction="Synchronise" getEnabled="CanOpenBook" supertip="Check this manuscript using the existing chapter synchronisation workflow." />
|
||||
<button id="PlotDirectorRepair" label="Repair Synchronisation" onAction="Repair" getEnabled="CanOpenBook" supertip="Validate or repair the existing import while preserving existing chapters, scenes and author decisions." />
|
||||
</group>
|
||||
<group id="PlotDirectorConnection" label="Connection">
|
||||
<labelControl id="PlotDirectorStatus" getLabel="GetStatus" />
|
||||
<button id="PlotDirectorOpen" label="Open Companion" imageMso="FileOpen" onAction="OpenCompanion" />
|
||||
<menu id="PlotDirectorTools" label="Tools">
|
||||
<button id="PlotDirectorRemoveLegacyMarkers" label="Remove Legacy Markers" onAction="RemoveLegacyMarkers" supertip="Remove old PlotDirector chapter and scene anchors while keeping the manuscript and its identity." />
|
||||
<button id="PlotDirectorTroubleshooting" label="Troubleshooting" onAction="Troubleshooting" supertip="Inspect advanced Word connection, navigation and synchronisation diagnostics." />
|
||||
</menu>
|
||||
</group>
|
||||
</tab>
|
||||
</tabs>
|
||||
</ribbon>
|
||||
</customUI>
|
||||
|
||||
@ -583,7 +583,7 @@ namespace PlotDirector.WordAddIn
|
||||
if(active!=null)link.TryPost(new Message{Kind="metrics",SessionId=active.Session,Revision=active.Revision,Metrics=DrainMetrics(),QueueDepth=link.QueueDepth,OperationMessage=Diagnostics});
|
||||
}
|
||||
}
|
||||
private void RegisterState(LocalDocument doc)=>link.Register(Descriptor(doc));
|
||||
private void RegisterState(LocalDocument doc) { link.Register(Descriptor(doc)); if (active == doc) invalidateRibbon(); }
|
||||
private void ReportReadFailure(LocalDocument doc,string reason){CancelExtraction(doc);link.TryPost(new Message{Kind="adapter-result",SessionId=doc.Session,Revision=doc.Revision,OperationMessage=reason});lastResult=reason;}
|
||||
private void StopAi(LocalDocument doc,string reason)
|
||||
{
|
||||
|
||||
@ -8,16 +8,94 @@ namespace PlotDirector.WordAddIn
|
||||
{
|
||||
public sealed partial class WordAdapter
|
||||
{
|
||||
private string serverBook="",serverChapter="",serverScene="";private ChapterSyncProgress serverChapterProgress;
|
||||
public string ChapterStatusLabel=>active==null?"No active manuscript":serverChapterProgress==null?"Chapters not yet checked":$"Chapters {serverChapterProgress.Matching}/{serverChapterProgress.Total} · {serverChapterProgress.State}";
|
||||
public string ChapterStatusTip=>string.Join("\n",new[]{serverBook,serverChapter,serverScene,serverChapterProgress==null?"Connect and validate the current chapter.":serverChapterProgress.Message});
|
||||
private string serverBook = "", serverChapter = "", serverScene = "";
|
||||
private int serverBookId;
|
||||
private long serverRevision = -1;
|
||||
private ChapterSyncProgress serverChapterProgress;
|
||||
|
||||
public bool HasRibbonBook => active != null && ConnectionState == "connected" && serverBookId > 0;
|
||||
public string RibbonBook => HasRibbonBook && serverBook.Length > 0 ? serverBook : "No linked book";
|
||||
private bool HasCurrentRibbonState => HasRibbonBook && active.Revision == serverRevision;
|
||||
public string RibbonLocation => HasCurrentRibbonState && serverScene.Length > 0
|
||||
? (serverChapter.Length == 0 ? serverScene : serverChapter + " · " + serverScene)
|
||||
: "Location unverified";
|
||||
|
||||
public string ChapterStatusLabel
|
||||
{
|
||||
get
|
||||
{
|
||||
if (ConnectionState != "connected") return "Not connected";
|
||||
if (!HasRibbonBook) return "No linked book";
|
||||
if (!HasCurrentRibbonState) return "Changes found";
|
||||
if (serverChapterProgress == null) return "Not yet checked";
|
||||
switch (serverChapterProgress.State)
|
||||
{
|
||||
case "Checking": return "Synchronising";
|
||||
case "Synchronised":
|
||||
if (serverChapterProgress.Total > 0 && serverChapterProgress.Matching == serverChapterProgress.Total)
|
||||
return "Up to date";
|
||||
return "Review needed";
|
||||
case "Needs review": return "Review needed";
|
||||
case "Changed": return "Changes found";
|
||||
default: return "Not yet checked";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string RibbonSyncProgress => ChapterStatusLabel == "Synchronising" && serverChapterProgress.Total > 0
|
||||
? string.Format("{0} of {1} chapters accepted", serverChapterProgress.Matching, serverChapterProgress.Total)
|
||||
: " ";
|
||||
public string ChapterStatusTip => (HasCurrentRibbonState ? serverChapterProgress?.Message : null)
|
||||
?? "Synchronise to verify the manuscript position.";
|
||||
|
||||
public void OpenWritingWorkspace(string command)
|
||||
{
|
||||
if (HasRibbonBook)
|
||||
link.TryPost(new Message { Kind = command, SessionId = active.Session, Revision = active.Revision });
|
||||
}
|
||||
|
||||
public void RequestChapterSynchronisation(bool repair)
|
||||
{if(active==null||ConnectionState!="connected"){lastResult="Open a manuscript and connect Companion first.";return;}if(!link.TryPost(new Message{Kind=repair?"tray-repair":"tray-sync",SessionId=active.Session,Revision=active.Revision}))lastResult="Local queue is busy. Word remains usable; retry shortly.";}
|
||||
public void ShowTroubleshooting()=>System.Windows.Forms.MessageBox.Show(Diagnostics,"PlotDirector Troubleshooting");
|
||||
{
|
||||
if (!HasRibbonBook) { lastResult = "Open a linked manuscript and connect Companion first."; return; }
|
||||
if (!link.TryPost(new Message { Kind = repair ? "tray-repair" : "tray-sync", SessionId = active.Session, Revision = active.Revision }))
|
||||
{
|
||||
lastResult = "Local queue is busy. Word remains usable; retry shortly.";
|
||||
return;
|
||||
}
|
||||
serverChapterProgress = null;
|
||||
invalidateRibbon();
|
||||
}
|
||||
|
||||
public void ShowTroubleshooting() => System.Windows.Forms.MessageBox.Show(Diagnostics, "PlotDirector Troubleshooting");
|
||||
|
||||
private void ReceiveChapterStatus(Message message)
|
||||
{if(active==null||active.Session!=message.SessionId||active.Revision!=message.Revision)return;var before=ChapterStatusLabel+ChapterStatusTip;serverBook=message.BookTitle;serverChapter=message.ChapterTitle;serverScene=message.SceneTitle;serverChapterProgress=message.ChapterProgress;if(before!=ChapterStatusLabel+ChapterStatusTip)invalidateRibbon();}
|
||||
{
|
||||
if (active == null || active.Session != message.SessionId || active.Revision != message.Revision) return;
|
||||
if (serverBookId == message.BookId && serverRevision == message.Revision &&
|
||||
serverBook == message.BookTitle && serverChapter == message.ChapterTitle && serverScene == message.SceneTitle &&
|
||||
serverChapterProgress?.State == message.ChapterProgress?.State &&
|
||||
serverChapterProgress?.Total == message.ChapterProgress?.Total &&
|
||||
serverChapterProgress?.Matching == message.ChapterProgress?.Matching &&
|
||||
serverChapterProgress?.Message == message.ChapterProgress?.Message) return;
|
||||
serverBookId = message.BookId;
|
||||
serverRevision = message.Revision;
|
||||
serverBook = message.BookTitle ?? "";
|
||||
serverChapter = message.ChapterTitle ?? "";
|
||||
serverScene = message.SceneTitle ?? "";
|
||||
serverChapterProgress = message.ChapterProgress;
|
||||
invalidateRibbon();
|
||||
}
|
||||
|
||||
private void ChangeActiveDocument(LocalDocument document)
|
||||
{if(active==document)return;active=document;serverBook=serverChapter=serverScene="";serverChapterProgress=null;invalidateRibbon();}
|
||||
{
|
||||
if (active == document) return;
|
||||
active = document;
|
||||
serverBookId = 0;
|
||||
serverRevision = -1;
|
||||
serverBook = serverChapter = serverScene = "";
|
||||
serverChapterProgress = null;
|
||||
invalidateRibbon();
|
||||
}
|
||||
private sealed class ChapterRead
|
||||
{
|
||||
public AdapterCommand Command;public int Phase,Seek,Steps,Start,End,Offset;public string Text="",Xml="",NextId="";public bool Begun;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user