Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix: import markdown to text note
SiriusXT507910b0ce4b
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.tsindex ce33d14470..f52f2e4988 100644--- a/apps/client/src/components/app_context.ts+++ b/apps/client/src/components/app_context.ts@@ -218,7 +218,6 @@ export type CommandMappings = {/** Works only in the electron context menu. */replaceMisspelling: CommandData;- importMarkdownInline: CommandData;showPasswordNotSet: CommandData;showProtectedSessionPasswordDialog: CommandData;showUploadAttachmentsDialog: CommandData & { noteId: string };@@ -306,7 +305,7 @@ export type CommandMappings = {addLinkToText: CommandData;followLinkUnderCursor: CommandData;insertDateTimeToText: CommandData;- pasteMarkdownIntoText: CommandData;+ showMarkdownIntoTextDialog: CommandData & { textTypeWidget: EditableTextTypeWidget };cutIntoNote: CommandData;addIncludeNoteToText: CommandData;editReadOnlyNote: CommandData;diff --git a/apps/client/src/services/glob.ts b/apps/client/src/services/glob.tsindex 48d0d29a7d..44ce643094 100644--- a/apps/client/src/services/glob.ts+++ b/apps/client/src/services/glob.ts@@ -20,9 +20,6 @@ function setupGlobs() {window.glob.froca = froca;window.glob.treeCache = froca; // compatibility for CKEditor builds for a while- // for CKEditor integration (button on block toolbar)- window.glob.importMarkdownInline = async () => appContext.triggerCommand("importMarkdownInline");-window.onerror = function (msg, url, lineNo, columnNo, error) {const string = String(msg).toLowerCase();diff --git a/apps/client/src/types.d.ts b/apps/client/src/types.d.tsindex c5a93bd0ab..d283983b48 100644--- a/apps/client/src/types.d.ts+++ b/apps/client/src/types.d.ts@@ -26,7 +26,6 @@ interface CustomGlobals {appContext: AppContext;froca: Froca;treeCache: Froca;- importMarkdownInline: () => Promise<unknown>;SEARCH_HELP_TEXT: string;activeDialog: JQuery<HTMLElement> | null;componentId: string;diff --git a/apps/client/src/widgets/dialogs/markdown_import.tsx b/apps/client/src/widgets/dialogs/markdown_import.tsxindex d14d6fb117..5676d7298f 100644--- a/apps/client/src/widgets/dialogs/markdown_import.tsx+++ b/apps/client/src/widgets/dialogs/markdown_import.tsx@@ -7,6 +7,7 @@ import utils from "../../services/utils";import Modal from "../react/Modal";import Button from "../react/Button";import { useTriliumEvent } from "../react/hooks";+import EditableTextTypeWidget from "../type_widgets/editable_text";interface RenderMarkdownResponse {htmlContent: string;@@ -14,29 +15,27 @@ interface RenderMarkdownResponse {export default function MarkdownImportDialog() {const markdownImportTextArea = useRef<HTMLTextAreaElement>(null);+ const [textTypeWidget, setTextTypeWidget] = useState<EditableTextTypeWidget>();const [ text, setText ] = useState("");const [ shown, setShown ] = useState(false);- const triggerImport = useCallback(() => {- if (appContext.tabManager.getActiveContextNoteType() !== "text") {- return;- }-+ useTriliumEvent("showMarkdownIntoTextDialog", ({ textTypeWidget }) => {+ setTextTypeWidget(textTypeWidget);if (utils.isElectron()) {const { clipboard } = utils.dynamicRequire("electron");const text = clipboard.readText();- convertMarkdownToHtml(text);+ convertMarkdownToHtml(text, textTypeWidget);} else {setShown(true);}- }, []);-- useTriliumEvent("importMarkdownInline", triggerImport);- useTriliumEvent("pasteMarkdownIntoText", triggerImport);+ });async function sendForm() {- await convertMarkdownToHtml(text);+ if (textTypeWidget) {+ await convertMarkdownToHtml(text, textTypeWidget);+ }+setText("");setShown(false);}@@ -44,7 +43,7 @@ export default function MarkdownImportDialog() {return (<ModalclassName="markdown-import-dialog" title={t("markdown_import.dialog_title")} size="lg"- footer={<Button className="markdown-import-button" text={t("markdown_import.import_button")} onClick={sendForm} keyboardShortcut="Ctrl+Space" />}+ footer={<Button className="markdown-import-button" text={t("markdown_import.import_button")} onClick={sendForm} keyboardShortcut="Ctrl+Enter" />}onShown={() => markdownImportTextArea.current?.focus()}onHidden={() => setShown(false) }show={shown}@@ -63,19 +62,10 @@ export default function MarkdownImportDialog() {)}-async function convertMarkdownToHtml(markdownContent: string) {+async function convertMarkdownToHtml(markdownContent: string, textTypeWidget: EditableTextTypeWidget) {const { htmlContent } = await server.post<RenderMarkdownResponse>("other/render-markdown", { markdownContent });- const textEditor = await appContext.tabManager.getActiveContext()?.getTextEditor();- if (!textEditor) {- return;- }-- const viewFragment = textEditor.data.processor.toView(htmlContent);- const modelFragment = textEditor.data.toModel(viewFragment);-- textEditor.model.insertContent(modelFragment, textEditor.model.document.selection);- textEditor.editing.view.focus();-+ await textTypeWidget.addHtmlToEditor(htmlContent);+toast.showMessage(t("markdown_import.import_success"));}\ No newline at end of filediff --git a/apps/client/src/widgets/type_widgets/editable_text.ts b/apps/client/src/widgets/type_widgets/editable_text.tsindex 8a2103c1b2..ee76a107c1 100644--- a/apps/client/src/widgets/type_widgets/editable_text.ts+++ b/apps/client/src/widgets/type_widgets/editable_text.ts@@ -329,6 +329,30 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {});}+ async addHtmlToEditor(html: string) {+ await this.initialized;++ const editor = this.watchdog.editor;+ if (!editor) return;++ editor.model.change((writer) => {+ const viewFragment = editor.data.processor.toView(html);+ const modelFragment = editor.data.toModel(viewFragment);+ const insertPosition = editor.model.document.selection.getLastPosition();++ if (insertPosition) {+ const range = editor.model.insertContent(modelFragment, insertPosition);++ if (range) {+ writer.setSelection(range.end);+ }+ }++ });++ editor.editing.view.focus();+ }+addTextToActiveEditorEvent({ text }: EventData<"addTextToActiveEditor">) {if (!this.isActive()) {return;@@ -385,6 +409,10 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {this.triggerCommand("showAddLinkDialog", { textTypeWidget: this, text: selectedText });}+ pasteMarkdownIntoTextCommand() {+ this.triggerCommand("showMarkdownIntoTextDialog", { textTypeWidget: this });+ }+getSelectedText() {const range = this.watchdog.editor?.model.document.selection.getFirstRange();let text = "";diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.tsindex c1018db375..6f05b9f2a9 100644--- a/packages/ckeditor5/src/augmentation.ts+++ b/packages/ckeditor5/src/augmentation.ts@@ -19,6 +19,5 @@ declare global {getHeaders(): Promise<Record<string, string>>;getReferenceLinkTitle(href: string): Promise<string>;getReferenceLinkTitleSync(href: string): string;- importMarkdownInline(): void;}}diff --git a/packages/ckeditor5/src/plugins/markdownimport.ts b/packages/ckeditor5/src/plugins/markdownimport.tsindex c00b9f7f6d..02f208e1d8 100644--- a/packages/ckeditor5/src/plugins/markdownimport.ts+++ b/packages/ckeditor5/src/plugins/markdownimport.ts@@ -31,7 +31,10 @@ export default class MarkdownImportPlugin extends Plugin {class ImportMarkdownCommand extends Command {execute() {- glob.importMarkdownInline();+ const editorEl = this.editor.editing.view.getDomRoot();+ const component = glob.getComponentByEl(editorEl);++ component.triggerCommand('pasteMarkdownIntoText');}}