fix: refresh template editor after importing

This commit is contained in:
windingwind 2024-11-19 17:01:47 +01:00
parent 67ffda4830
commit 88e831d395
7 changed files with 49 additions and 6 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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并向您展示如何充分利用其功能。

View File

@ -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();
}
}

View File

@ -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 = [