From 1873c1bda9ac6af362852870df4e09e638b2f6d3 Mon Sep 17 00:00:00 2001 From: NishikawaKoharu Date: Tue, 10 Nov 2026 09:13:32 +0900 Subject: [PATCH] =?UTF-8?q?Wiki=E3=81=AE=E7=89=88=E5=88=87=E6=9B=BF?= =?UTF-8?q?=E3=81=A7=E7=B7=A8=E9=9B=86=E3=82=A8=E3=83=AA=E3=82=A2=E3=81=8C?= =?UTF-8?q?=E7=A9=BA=E3=81=AB=E3=81=AA=E3=82=8B=E4=B8=8D=E5=85=B7=E5=90=88?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/wiki/static/wikiPageMilkdown.js | 42 ++++++++++++++------------ 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/addons/wiki/static/wikiPageMilkdown.js b/addons/wiki/static/wikiPageMilkdown.js index 2be8cd1852b..cbf6ff0b8d2 100644 --- a/addons/wiki/static/wikiPageMilkdown.js +++ b/addons/wiki/static/wikiPageMilkdown.js @@ -44,6 +44,18 @@ const wikiId = (wikiCtx.wiki.wikiName === 'home') ? wikiCtx.node.id : window.con const wsPrefix = (window.location.protocol === 'https:') ? 'wss://' : 'ws://'; const wsUrl = wsPrefix + window.contextVars.wiki.urls.y_websocket; +function getEditorMarkdown() { + var content = ''; + if (mEdit && typeof mEdit.action === 'function') { + mEdit.action((ctx) => { + const view = ctx.get(mCore.editorViewCtx); + const serializer = ctx.get(mCore.serializerCtx); + content = serializer(view.state.doc); + }); + } + return content; +} + import { underlineSchema, underlineInputRule, remarkUnderlineToMarkdown, remarkUnderlineFromMarkdown, toggleUnderlineCommand } from './underline.js'; import { colortextSchema, colortextInputRule, remarkColortextToMarkdown, remarkColortextFromMarkdown, toggleColortextCommand } from './colortext.js'; import { extendedImageSchemaPlugin, extendedInsertImageCommand, extendedUpdateImageCommand, updatedInsertImageInputRule } from './image.js'; @@ -239,15 +251,11 @@ function ViewWidget(visible, version, viewText, rendered, contentURL, allowMathj var requestURL; if (typeof self.version() !== 'undefined') { if (self.version() === 'preview') { - var toMarkdown = ''; - if (mEdit !== undefined) { - mEdit.action((ctx) => { - const view = ctx.get(mCore.editorViewCtx); - const serializer = ctx.get(mCore.serializerCtx); - toMarkdown = serializer(view.state.doc); - }); + var toMarkdown = getEditorMarkdown(); + // Avoid wiping a preserved draft when the editor was destroyed for version view. + if (toMarkdown) { + self.displaySource(toMarkdown); } - self.displaySource(toMarkdown); if (document.getElementById("editWysiwyg").style.display === "none"){ document.getElementById("mMenuBar").style.display = ""; document.getElementById("mEditorFooter").style.display = ""; @@ -256,11 +264,9 @@ function ViewWidget(visible, version, viewText, rendered, contentURL, allowMathj document.getElementById("wikiViewRender").style.display = "none"; } else { document.getElementById("mMenuBar").style.display = "none"; + // Hide the live editor while viewing another version. Do not destroy + // its DOM/instance; recreating collab state can leave an empty editor. document.getElementById("mEditor").style.display = "none"; - const milkdownDivs = document.getElementById("mEditor").querySelectorAll('div.milkdown'); - if (milkdownDivs.length > 0) { - milkdownDivs[0].remove(); - } document.getElementById("mEditorFooter").style.display = "none"; document.getElementById("wikiViewRender").style.display = ""; if (self.version() === 'current') { @@ -545,13 +551,11 @@ function ViewModel(options){ if (panel === 'compare') { if(display && self.compareVis()){ self.viewVersion('preview'); - var toMarkdown = ''; - mEdit.action((ctx) => { - const view = ctx.get(mCore.editorViewCtx); - const serializer = ctx.get(mCore.serializerCtx); - toMarkdown = serializer(view.state.doc); - }); - self.viewVM.displaySource(toMarkdown); + var toMarkdown = getEditorMarkdown(); + // Avoid clearing Compare's "after" side when the editor markdown is empty. + if (toMarkdown) { + self.viewVM.displaySource(toMarkdown); + } } }