Jelajahi Sumber

Improve Office import titles and link removal

Preserve the original filename in Office titles for read-only imports until the document is saved to its own path, while still suggesting the native Office extension on save. Also refactor Docs link removal into a shared helper and make the editor context menu offer “Remove link” directly when right-clicking an existing link.
Toby Chui 1 hari lalu
induk
melakukan
54fbab9269
2 mengubah file dengan 25 tambahan dan 5 penghapusan
  1. 10 1
      src/web/Office/common/office.js
  2. 15 4
      src/web/Office/docs/docs.js

+ 10 - 1
src/web/Office/common/office.js

@@ -110,6 +110,10 @@ var OfficeApp = (function () {
     var draftTimer = null;
     var autosaveTimer = null;
     var loadedFromImport = false;
+    // the file a read-only import came from (a .doc opened in Docs): the
+    // title shows it until the document is saved somewhere of its own,
+    // while filename holds the name Save suggests in the app's format
+    var sourceName = null;
     // saving is asynchronous (the app prepares the document, then it
     // travels): one write at a time, and an edit made while it is under way
     // keeps the document dirty
@@ -219,6 +223,7 @@ var OfficeApp = (function () {
                 delete m._sessionAt;
                 delete m._origin;
                 meta = m;
+                sourceName = null;
                 if (origin && origin.fp) {
                     filepath = origin.fp;
                     filename = origin.fn;
@@ -277,7 +282,7 @@ var OfficeApp = (function () {
 
     /* ---------- title / status ---------- */
     function updateTitle() {
-        var name = openingName || filename || (cfg.defaultFileName + cfg.extension);
+        var name = openingName || (!filepath && sourceName) || filename || (cfg.defaultFileName + cfg.extension);
         var t = name + (dirty ? " •" : "") + " - " + cfg.appName;
         OfficePlatform.setWindowTitle(t);
         var $dn = $(".of-docname");
@@ -495,6 +500,7 @@ var OfficeApp = (function () {
         meta = env.meta || {};
         loadedFromImport = false;
         openingName = null;
+        sourceName = null;
         /*
             A template is a starting point, not a file the person is editing:
             the content loads but the document stays unattached, so Ctrl+S
@@ -535,12 +541,14 @@ var OfficeApp = (function () {
     function adoptImportedPath(fp, fn) {
         loadedFromImport = true;
         openingName = null;
+        sourceName = null;
         if (saveFormatFor(extOf(fn))) {
             filepath = fp;
             filename = fn;
         } else {
             filepath = null;   // read-only import: force Save As on save
             filename = stripExt(fn) + cfg.extension;
+            sourceName = fn;
         }
     }
     function importedStatus(fn) {
@@ -686,6 +694,7 @@ var OfficeApp = (function () {
         var go = function () {
             filepath = null;
             filename = null;
+            sourceName = null;
             meta = { createdAt: now(), revision: 0 };
             loadedFromImport = false;
             cfg.create();

+ 15 - 4
src/web/Office/docs/docs.js

@@ -1586,6 +1586,13 @@
     }
 
     /* ================= links ================= */
+    // unwrap a link, keeping its text
+    function removeLink(a) {
+        if (!a || !a.parentNode) return;
+        while (a.firstChild) a.parentNode.insertBefore(a.firstChild, a);
+        a.parentNode.removeChild(a);
+        afterEdit(true);
+    }
     function linkDialog() {
         if (inHeaderFooter()) return;
         var existing = null;
@@ -1610,9 +1617,7 @@
                 label: "Remove link", danger: true,
                 action: function (close) {
                     close();
-                    while (existing.firstChild) existing.parentNode.insertBefore(existing.firstChild, existing);
-                    existing.parentNode.removeChild(existing);
-                    afterEdit(true);
+                    removeLink(existing);
                 }
             });
         }
@@ -5045,7 +5050,13 @@
             items.push({ label: "Underline", icon: "underline", key: "Ctrl+U", action: function () { exec("underline"); } });
             items.push({ label: "Clear formatting", icon: "eraser", action: clearFormatting });
             items.push({ sep: true });
-            items.push({ label: "Insert link...", icon: "linkify", key: "Ctrl+K", action: linkDialog });
+            // on a link the link entry takes it off instead of adding one
+            var linkHit = e.target.closest ? e.target.closest("a[href]") : null;
+            if (linkHit && editor.contains(linkHit)) {
+                items.push({ label: "Remove link", icon: "unlinkify", action: function () { removeLink(linkHit); } });
+            } else {
+                items.push({ label: "Insert link...", icon: "linkify", key: "Ctrl+K", action: linkDialog });
+            }
             items.push({ label: "Insert page break", icon: "file outline", key: "Ctrl+Enter", action: insertPageBreak });
             items.push({ sep: true });
             items.push({ label: "Select all", icon: "i cursor", key: "Ctrl+A", action: selectAll });