From 88e831d39503fefb8fe640024b154cf8cf979c20 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:01:47 +0100 Subject: [PATCH] fix: refresh template editor after importing --- addon/locale/en-US/addon.ftl | 1 + addon/locale/it-IT/addon.ftl | 1 + addon/locale/ru-RU/addon.ftl | 1 + addon/locale/tr-TR/addon.ftl | 1 + addon/locale/zh-CN/addon.ftl | 1 + src/modules/template/controller.ts | 3 ++ src/modules/template/editorWindow.ts | 47 ++++++++++++++++++++++++---- 7 files changed, 49 insertions(+), 6 deletions(-) diff --git a/addon/locale/en-US/addon.ftl b/addon/locale/en-US/addon.ftl index c9acf25..8702b59 100644 --- a/addon/locale/en-US/addon.ftl +++ b/addon/locale/en-US/addon.ftl @@ -101,6 +101,7 @@ alert-notValidParentItemError = No valid parent item. alert-syncImportedNotes = Keep imported notes in sync with MarkDown files? alert-linkCreator-emptyNote = Cannot create link from/to an empty note. alert-templateEditor-shouldImport = Seems like you are trying to directly save a note template share code. Do you want to import it as a template? +alert-templateEditor-unsaved = You have unsaved changes in the template editor. Do you want to save them? userGuide-start-title = Welcome to Better Notes! userGuide-start-desc = Better Notes is a powerful note-taking tool that helps you organize your thoughts and ideas while reading papers. This guide will help you get started with Better Notes and show you how to make the most of its features. diff --git a/addon/locale/it-IT/addon.ftl b/addon/locale/it-IT/addon.ftl index 00dde5d..67fe254 100644 --- a/addon/locale/it-IT/addon.ftl +++ b/addon/locale/it-IT/addon.ftl @@ -97,6 +97,7 @@ alert-notValidParentItemError = Nessun elemento genitore valido. alert-syncImportedNotes = Si desidera sincronizzare le note importate con i file markdown? alert-linkCreator-emptyNote = Non è possibile creare un link da/a una nota vuota. alert-templateEditor-shouldImport = Sembra che tu stia cercando di salvare direttamente un codice di condivisione di un template di nota. Vuoi importarlo come template? +alert-templateEditor-unsaved = You have unsaved changes in the template editor. Do you want to save them? userGuide-start-title = Questo è Better Notes! userGuide-start-desc = Better Notes è un potente strumento di gestione delle note che ti può aiutare a organizzare pensieri e idee durante la lettura di articoli scientifici. Questa guida ti aiuterà a prendere confidenza con Better Notes e ti mostrerà come usare al meglio le sue funzioni. diff --git a/addon/locale/ru-RU/addon.ftl b/addon/locale/ru-RU/addon.ftl index 203df51..bede468 100644 --- a/addon/locale/ru-RU/addon.ftl +++ b/addon/locale/ru-RU/addon.ftl @@ -101,6 +101,7 @@ alert-notValidParentItemError=Нет валидного родительског alert-syncImportedNotes = Синхронизировать импортированные заметки с файлами MarkDown? alert-linkCreator-emptyNote = Cannot create link from/to an empty note. alert-templateEditor-shouldImport = Вы пытаетесь сохранить код шаблона заметки. Хотите импортировать его как шаблон? +alert-templateEditor-unsaved = You have unsaved changes in the template editor. Do you want to save them? userGuide-start-title = Welcome to Better Notes! userGuide-start-desc = Better Notes is a powerful note-taking tool that helps you organize your thoughts and ideas while reading papers. This guide will help you get started with Better Notes and show you how to make the most of its features. diff --git a/addon/locale/tr-TR/addon.ftl b/addon/locale/tr-TR/addon.ftl index 3a8a601..8615b99 100644 --- a/addon/locale/tr-TR/addon.ftl +++ b/addon/locale/tr-TR/addon.ftl @@ -101,6 +101,7 @@ alert-notValidParentItemError = Geçerli ana eser yok. alert-syncImportedNotes = İçe aktarılmış notları Markdown dosyalarıyla eşitlemede tutmak ister misiniz? alert-linkCreator-emptyNote = Boş bir nota/nottan bağlantı oluşturulamaz. note. alert-templateEditor-shouldImport = Şablon kodunu doğrudan kaydetmeye çalışıyorsunuz gibi görünüyor. Şablon olarak içe aktarmak ister misiniz? +alert-templateEditor-unsaved = You have unsaved changes in the template editor. Do you want to save them? userGuide-start-title = Better Notes'a hoş geldiniz! userGuide-start-desc = Better Notes düşüncelerinizi ve tasarılarınızı düzenlemenize yardımcı olan güçlü bir not alma aracıdır. Kullanımı kolay ve yalın biçimde tasarlanmış olsa da karmaşık not alma işlerini görebilecek kadar da esnektir. Bu rehber size Better Notes'u kullanmaya başlamanıza ve ileri düzey işlevleri verimli kullanmanıza yardımcı olacaktır. diff --git a/addon/locale/zh-CN/addon.ftl b/addon/locale/zh-CN/addon.ftl index dfd8b43..dce43f2 100644 --- a/addon/locale/zh-CN/addon.ftl +++ b/addon/locale/zh-CN/addon.ftl @@ -101,6 +101,7 @@ alert-notValidParentItemError=无效的父条目。 alert-syncImportedNotes = 保持导入的笔记与 MarkDown 文件同步? alert-linkCreator-emptyNote = 无法从/向空笔记创建链接。 alert-templateEditor-shouldImport = 似乎您正在尝试直接保存一个笔记模板分享代码。您想要将其导入为模板吗? +alert-templateEditor-unsaved = 您在模板编辑器中有未保存的更改。您想要保存它们吗? userGuide-start-title = 欢迎使用Better Notes! userGuide-start-desc = Better Notes是一个强大的笔记工具,帮助您组织阅读论文时的概念和想法。本指南将帮助您开始使用Better Notes,并向您展示如何充分利用其功能。 diff --git a/src/modules/template/controller.ts b/src/modules/template/controller.ts index 6ebe200..98eef63 100644 --- a/src/modules/template/controller.ts +++ b/src/modules/template/controller.ts @@ -88,4 +88,7 @@ function importTemplateFromClipboard(text?: string) { } setTemplate({ name: template.name, text: template.content }); showHint(`Template ${template.name} saved.`); + if (addon.data.template.editor.window) { + addon.data.template.editor.window.refresh(); + } } diff --git a/src/modules/template/editorWindow.ts b/src/modules/template/editorWindow.ts index b0c07c1..be52ce4 100644 --- a/src/modules/template/editorWindow.ts +++ b/src/modules/template/editorWindow.ts @@ -65,7 +65,7 @@ export async function showTemplateEditor() { (Zotero.isMac && event.key == "Backspace") ) { addon.api.template.removeTemplate(getSelectedTemplateName()); - refresh(); + refresh(true); return false; } return true; @@ -193,6 +193,8 @@ export async function showTemplateEditor() { ?.addEventListener("command", (ev) => { updateSnippets((ev.target as XULMenuListElement)?.value); }); + // An ugly hack to make the editor refresh exposed + _window.refresh = refresh; addon.data.template.editor.window?.focus(); const editorWin = (_window.document.querySelector("#editor") as any) .contentWindow; @@ -209,7 +211,18 @@ export async function showTemplateEditor() { } } -async function refresh() { +async function refresh(force = false) { + const win = addon.data.template.editor.window; + if (!win) { + return; + } + if (!force && isTemplateNotSaved()) { + const save = win.confirm(getString("alert-templateEditor-unsaved")); + if (save) { + saveSelectedTemplate(); + return; + } + } updateData(); updateTable(); updateEditor(); @@ -259,6 +272,28 @@ function getRowLabelColor(type: string) { } } +function isTemplateNotSaved() { + const name = getSelectedTemplateName(); + if (!name) { + return false; + } + const text = addon.data.template.editor.editor?.getValue() as string; + const savedText = addon.api.template.getTemplateText(name); + if (text !== savedText) { + return true; + } + const { type, name: displayName } = getRowData(getSelectedIndex()); + const templateType = + addon.data.template.editor.window?.document.querySelector( + "#editor-type", + ) as XULMenuListElement; + const templateName = + addon.data.template.editor.window?.document.querySelector( + "#editor-name", + ) as HTMLInputElement; + return type !== templateType.value || displayName !== templateName.value; +} + function updateData() { addon.data.template.editor.templates = addon.api.template.getTemplateKeys(); } @@ -582,7 +617,7 @@ function saveSelectedTemplate() { ); if (useImport) { addon.hooks.onImportTemplateFromClipboard(template.text); - refresh(); + refresh(true); return; } } @@ -596,7 +631,7 @@ function saveSelectedTemplate() { addon.data.template.editor.tableHelper?.treeInstance.selection.selected .values() .next().value; - refresh().then(() => updateTable(selectedId)); + refresh(true).then(() => updateTable(selectedId)); } function deleteSelectedTemplate() { @@ -608,7 +643,7 @@ function deleteSelectedTemplate() { return; } addon.api.template.removeTemplate(name); - refresh(); + refresh(true); } function resetSelectedTemplate() { @@ -698,7 +733,7 @@ async function restoreTemplates(win: Window) { } addon.api.template.setTemplate(t); } - await refresh(); + await refresh(true); } const formatStore = [