update: use LargePrefHelper
This commit is contained in:
parent
d50b724b7d
commit
d2ef64634f
|
|
@ -67,7 +67,7 @@
|
||||||
"unist-util-visit": "^5.0.0",
|
"unist-util-visit": "^5.0.0",
|
||||||
"unist-util-visit-parents": "^6.0.1",
|
"unist-util-visit-parents": "^6.0.1",
|
||||||
"yamljs": "^0.3.0",
|
"yamljs": "^0.3.0",
|
||||||
"zotero-plugin-toolkit": "^2.2.5"
|
"zotero-plugin-toolkit": "^2.3.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
|
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
|
||||||
|
|
|
||||||
41
src/addon.ts
41
src/addon.ts
|
|
@ -3,6 +3,7 @@ import {
|
||||||
ColumnOptions,
|
ColumnOptions,
|
||||||
VirtualizedTableHelper,
|
VirtualizedTableHelper,
|
||||||
} from "zotero-plugin-toolkit/dist/helpers/virtualizedTable";
|
} from "zotero-plugin-toolkit/dist/helpers/virtualizedTable";
|
||||||
|
import { LargePrefHelper } from "zotero-plugin-toolkit/dist/helpers/largePref";
|
||||||
import ToolkitGlobal from "zotero-plugin-toolkit/dist/managers/toolkitGlobal";
|
import ToolkitGlobal from "zotero-plugin-toolkit/dist/managers/toolkitGlobal";
|
||||||
|
|
||||||
import { getPref, setPref } from "./utils/prefs";
|
import { getPref, setPref } from "./utils/prefs";
|
||||||
|
|
@ -30,6 +31,7 @@ class Addon {
|
||||||
pdf: { promise?: _ZoteroTypes.PromiseObject };
|
pdf: { promise?: _ZoteroTypes.PromiseObject };
|
||||||
};
|
};
|
||||||
sync: {
|
sync: {
|
||||||
|
data?: LargePrefHelper;
|
||||||
lock: boolean;
|
lock: boolean;
|
||||||
manager: {
|
manager: {
|
||||||
window?: Window;
|
window?: Window;
|
||||||
|
|
@ -73,15 +75,18 @@ class Addon {
|
||||||
top: number;
|
top: number;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
templateEditor: {
|
template: {
|
||||||
window?: Window;
|
data?: LargePrefHelper;
|
||||||
tableHelper?: VirtualizedTableHelper;
|
editor: {
|
||||||
editor?: any;
|
window?: Window;
|
||||||
templates: { name: string }[];
|
tableHelper?: VirtualizedTableHelper;
|
||||||
};
|
editor?: any;
|
||||||
templatePicker: {
|
templates: string[];
|
||||||
mode: "insert" | "create" | "export";
|
};
|
||||||
data: Record<string, any>;
|
picker: {
|
||||||
|
mode: "insert" | "create" | "export";
|
||||||
|
data: Record<string, any>;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
readonly prompt?: Prompt;
|
readonly prompt?: Prompt;
|
||||||
} = {
|
} = {
|
||||||
|
|
@ -141,14 +146,16 @@ class Addon {
|
||||||
pined: false,
|
pined: false,
|
||||||
anchorPosition: undefined,
|
anchorPosition: undefined,
|
||||||
},
|
},
|
||||||
templateEditor: {
|
template: {
|
||||||
window: undefined,
|
editor: {
|
||||||
tableHelper: undefined,
|
window: undefined,
|
||||||
templates: [],
|
tableHelper: undefined,
|
||||||
},
|
templates: [],
|
||||||
templatePicker: {
|
},
|
||||||
mode: "insert",
|
picker: {
|
||||||
data: {},
|
mode: "insert",
|
||||||
|
data: {},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
get prompt() {
|
get prompt() {
|
||||||
return ToolkitGlobal.getInstance().prompt.instance;
|
return ToolkitGlobal.getInstance().prompt.instance;
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,6 @@ import {
|
||||||
getTemplateKeys,
|
getTemplateKeys,
|
||||||
getTemplateText,
|
getTemplateText,
|
||||||
setTemplate,
|
setTemplate,
|
||||||
initTemplates,
|
|
||||||
removeTemplate,
|
removeTemplate,
|
||||||
} from "./modules/template/controller";
|
} from "./modules/template/controller";
|
||||||
import {
|
import {
|
||||||
|
|
@ -103,7 +102,6 @@ const template = {
|
||||||
getTemplateKeys,
|
getTemplateKeys,
|
||||||
getTemplateText,
|
getTemplateText,
|
||||||
setTemplate,
|
setTemplate,
|
||||||
initTemplates,
|
|
||||||
removeTemplate,
|
removeTemplate,
|
||||||
renderTemplatePreview,
|
renderTemplatePreview,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,7 @@ import {
|
||||||
import { annotationTagAction } from "./modules/annotationTagAction";
|
import { annotationTagAction } from "./modules/annotationTagAction";
|
||||||
import { createZToolkit } from "./utils/ztoolkit";
|
import { createZToolkit } from "./utils/ztoolkit";
|
||||||
import { waitUtilAsync } from "./utils/wait";
|
import { waitUtilAsync } from "./utils/wait";
|
||||||
|
import { initSyncList } from "./modules/sync/api";
|
||||||
|
|
||||||
async function onStartup() {
|
async function onStartup() {
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
|
|
@ -68,6 +69,8 @@ async function onStartup() {
|
||||||
|
|
||||||
registerPrefsWindow();
|
registerPrefsWindow();
|
||||||
|
|
||||||
|
initSyncList();
|
||||||
|
|
||||||
setSyncing();
|
setSyncing();
|
||||||
|
|
||||||
await onMainWindowLoad(window);
|
await onMainWindowLoad(window);
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import YAML = require("yamljs");
|
import YAML = require("yamljs");
|
||||||
import { clearPref, getPref, setPref } from "../../utils/prefs";
|
import { getPref, setPref } from "../../utils/prefs";
|
||||||
import { getNoteLinkParams } from "../../utils/link";
|
import { getNoteLinkParams } from "../../utils/link";
|
||||||
import { config } from "../../../package.json";
|
import { config } from "../../../package.json";
|
||||||
import { fileExists, formatPath } from "../../utils/str";
|
import { fileExists, formatPath } from "../../utils/str";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
initSyncList,
|
||||||
getRelatedNoteIds,
|
getRelatedNoteIds,
|
||||||
removeSyncNote,
|
removeSyncNote,
|
||||||
isSyncNote,
|
isSyncNote,
|
||||||
|
|
@ -18,16 +19,31 @@ export {
|
||||||
getMDFileName,
|
getMDFileName,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function initSyncList() {
|
||||||
|
const rawKeys = getPref("syncNoteIds") as string;
|
||||||
|
if (!rawKeys.startsWith("[") || !rawKeys.endsWith("]")) {
|
||||||
|
const keys = rawKeys.split(",").map((id) => parseInt(id));
|
||||||
|
setPref("syncNoteIds", JSON.stringify(keys));
|
||||||
|
}
|
||||||
|
addon.data.sync.data = new ztoolkit.LargePref(
|
||||||
|
`${config.prefsPrefix}.syncNoteIds`,
|
||||||
|
`${config.prefsPrefix}.syncDetail-`,
|
||||||
|
"parser",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function getSyncNoteIds(): number[] {
|
function getSyncNoteIds(): number[] {
|
||||||
const ids = getPref("syncNoteIds") as string;
|
const keys = addon.data.sync.data?.getKeys();
|
||||||
return Zotero.Items.get(ids.split(",").map((id: string) => Number(id)))
|
if (!keys) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return Zotero.Items.get(keys)
|
||||||
.filter((item) => item.isNote())
|
.filter((item) => item.isNote())
|
||||||
.map((item) => item.id);
|
.map((item) => item.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isSyncNote(noteId: number): boolean {
|
function isSyncNote(noteId: number): boolean {
|
||||||
const syncNoteIds = getSyncNoteIds();
|
return !!addon.data.sync.data?.hasKey(String(noteId));
|
||||||
return syncNoteIds.includes(noteId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getRelatedNoteIds(noteId: number): Promise<number[]> {
|
async function getRelatedNoteIds(noteId: number): Promise<number[]> {
|
||||||
|
|
@ -49,34 +65,16 @@ async function getRelatedNoteIds(noteId: number): Promise<number[]> {
|
||||||
return allNoteIds;
|
return allNoteIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getRelatedNoteIdsFromNotes(
|
|
||||||
noteIds: number[],
|
|
||||||
): Promise<number[]> {
|
|
||||||
let allNoteIds: number[] = [];
|
|
||||||
for (const noteId of noteIds) {
|
|
||||||
allNoteIds = allNoteIds.concat(await getRelatedNoteIds(noteId));
|
|
||||||
}
|
|
||||||
return allNoteIds;
|
|
||||||
}
|
|
||||||
|
|
||||||
function addSyncNote(noteId: number) {
|
function addSyncNote(noteId: number) {
|
||||||
const ids = getSyncNoteIds();
|
addon.data.sync.data?.setKey(String(noteId));
|
||||||
if (ids.includes(noteId)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ids.push(noteId);
|
|
||||||
setPref("syncNoteIds", ids.join(","));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeSyncNote(noteId: number) {
|
function removeSyncNote(noteId: number) {
|
||||||
const ids = getSyncNoteIds();
|
addon.data.sync.data?.deleteKey(String(noteId));
|
||||||
setPref("syncNoteIds", ids.filter((id) => id !== noteId).join(","));
|
|
||||||
clearPref(`syncDetail-${noteId}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateSyncStatus(noteId: number, status: SyncStatus) {
|
function updateSyncStatus(noteId: number, status: SyncStatus) {
|
||||||
addSyncNote(noteId);
|
addon.data.sync.data?.setValue(String(noteId), status);
|
||||||
setPref(`syncDetail-${noteId}`, JSON.stringify(status));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNoteStatus(noteId: number) {
|
function getNoteStatus(noteId: number) {
|
||||||
|
|
@ -109,17 +107,18 @@ function getNoteStatus(noteId: number) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSyncStatus(noteId?: number): SyncStatus {
|
function getSyncStatus(noteId?: number): SyncStatus {
|
||||||
const defaultStatus = JSON.stringify({
|
const defaultStatus = {
|
||||||
path: "",
|
path: "",
|
||||||
filename: "",
|
filename: "",
|
||||||
md5: "",
|
md5: "",
|
||||||
noteMd5: "",
|
noteMd5: "",
|
||||||
lastsync: new Date().getTime(),
|
lastsync: new Date().getTime(),
|
||||||
itemID: -1,
|
itemID: -1,
|
||||||
});
|
};
|
||||||
const status = JSON.parse(
|
const status = {
|
||||||
(getPref(`syncDetail-${noteId}`) as string) || defaultStatus,
|
...defaultStatus,
|
||||||
);
|
...(addon.data.sync.data?.getValue(String(noteId)) as SyncStatus),
|
||||||
|
};
|
||||||
status.path = formatPath(status.path);
|
status.path = formatPath(status.path);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -210,7 +210,7 @@ async function runItemTemplate(
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getItemTemplateData() {
|
async function getItemTemplateData() {
|
||||||
const librarySelectedIds = addon.data.templatePicker.data.librarySelectedIds;
|
const librarySelectedIds = addon.data.template.picker.data.librarySelectedIds;
|
||||||
if (librarySelectedIds && librarySelectedIds.length !== 0) {
|
if (librarySelectedIds && librarySelectedIds.length !== 0) {
|
||||||
const firstSelectedItem = Zotero.Items.get(librarySelectedIds[0]);
|
const firstSelectedItem = Zotero.Items.get(librarySelectedIds[0]);
|
||||||
const data = {} as Record<string, any>;
|
const data = {} as Record<string, any>;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import YAML = require("yamljs");
|
import YAML = require("yamljs");
|
||||||
import { clearPref, getPref, setPref } from "../../utils/prefs";
|
import { getPref } from "../../utils/prefs";
|
||||||
import { showHint } from "../../utils/hint";
|
import { showHint } from "../../utils/hint";
|
||||||
|
import { config } from "../../../package.json";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
getTemplateKeys,
|
getTemplateKeys,
|
||||||
|
|
@ -11,52 +12,46 @@ export {
|
||||||
importTemplateFromClipboard,
|
importTemplateFromClipboard,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Controller
|
function initTemplates() {
|
||||||
function getTemplateKeys(): { name: string }[] {
|
addon.data.template.data = new ztoolkit.LargePref(
|
||||||
const templateKeys = getPref("templateKeys") as string;
|
`${config.prefsPrefix}.templateKeys`,
|
||||||
return templateKeys ? JSON.parse(templateKeys) : [];
|
`${config.prefsPrefix}.template.`,
|
||||||
}
|
"parser",
|
||||||
|
);
|
||||||
function setTemplateKeys(templateKeys: { name: string }[]): void {
|
// Convert old template keys to new format
|
||||||
setPref("templateKeys", JSON.stringify(templateKeys));
|
const raw = getPref("templateKeys") as string;
|
||||||
}
|
let keys = raw ? JSON.parse(raw) : [];
|
||||||
|
if (keys.length > 0 && typeof keys[0] !== "string") {
|
||||||
function addTemplateKey(templateKey: { name: string }): boolean {
|
keys = keys.map((t: { name: string }) => t.name);
|
||||||
const templateKeys = getTemplateKeys();
|
setTemplateKeys(keys);
|
||||||
if (templateKeys.map((t) => t.name).includes(templateKey.name)) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
templateKeys.push(templateKey);
|
// Add default templates
|
||||||
setTemplateKeys(templateKeys);
|
const templateKeys = getTemplateKeys();
|
||||||
return true;
|
for (const defaultTemplate of addon.api.template.DEFAULT_TEMPLATES) {
|
||||||
|
if (!templateKeys.includes(defaultTemplate.name)) {
|
||||||
|
setTemplate(defaultTemplate, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addon.hooks.onUpdateTemplatePicker();
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeTemplateKey(keyName: string): boolean {
|
function getTemplateKeys(): string[] {
|
||||||
const templateKeys = getTemplateKeys();
|
return addon.data.template.data?.getKeys() || [];
|
||||||
if (!templateKeys.map((t) => t.name).includes(keyName)) {
|
}
|
||||||
return false;
|
|
||||||
}
|
function setTemplateKeys(templateKeys: string[]): void {
|
||||||
templateKeys.splice(templateKeys.map((t) => t.name).indexOf(keyName), 1);
|
addon.data.template.data?.setKeys(templateKeys);
|
||||||
setTemplateKeys(templateKeys);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTemplateText(keyName: string): string {
|
function getTemplateText(keyName: string): string {
|
||||||
let template = getPref(`template.${keyName}`) as string;
|
return addon.data.template.data?.getValue(keyName) || "";
|
||||||
if (!template) {
|
|
||||||
template = "";
|
|
||||||
setPref(`template.${keyName}`, template);
|
|
||||||
}
|
|
||||||
return template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setTemplate(
|
function setTemplate(
|
||||||
template: NoteTemplate,
|
template: NoteTemplate,
|
||||||
updatePrompt: boolean = true,
|
updatePrompt: boolean = true,
|
||||||
): void {
|
): void {
|
||||||
template = JSON.parse(JSON.stringify(template));
|
addon.data.template.data?.setValue(template.name, template.text);
|
||||||
addTemplateKey({ name: template.name });
|
|
||||||
setPref(`template.${template.name}`, template.text);
|
|
||||||
if (updatePrompt) {
|
if (updatePrompt) {
|
||||||
addon.hooks.onUpdateTemplatePicker();
|
addon.hooks.onUpdateTemplatePicker();
|
||||||
}
|
}
|
||||||
|
|
@ -66,27 +61,15 @@ function removeTemplate(
|
||||||
keyName: string | undefined,
|
keyName: string | undefined,
|
||||||
updatePrompt: boolean = true,
|
updatePrompt: boolean = true,
|
||||||
): void {
|
): void {
|
||||||
if (typeof keyName === "undefined") {
|
if (!keyName) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
removeTemplateKey(keyName);
|
addon.data.template.data?.deleteKey(keyName);
|
||||||
clearPref(`template.${keyName}`);
|
|
||||||
if (updatePrompt) {
|
if (updatePrompt) {
|
||||||
addon.hooks.onUpdateTemplatePicker();
|
addon.hooks.onUpdateTemplatePicker();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function initTemplates() {
|
|
||||||
const templateKeys = getTemplateKeys();
|
|
||||||
const currentNames = templateKeys.map((t) => t.name);
|
|
||||||
for (const defaultTemplate of addon.api.template.DEFAULT_TEMPLATES) {
|
|
||||||
if (!currentNames.includes(defaultTemplate.name)) {
|
|
||||||
setTemplate(defaultTemplate, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
addon.hooks.onUpdateTemplatePicker();
|
|
||||||
}
|
|
||||||
|
|
||||||
function importTemplateFromClipboard() {
|
function importTemplateFromClipboard() {
|
||||||
const templateText = Zotero.Utilities.Internal.getClipboard("text/unicode");
|
const templateText = Zotero.Utilities.Internal.getClipboard("text/unicode");
|
||||||
if (!templateText) {
|
if (!templateText) {
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,9 @@ import { waitUtilAsync } from "../../utils/wait";
|
||||||
|
|
||||||
export async function showTemplateEditor() {
|
export async function showTemplateEditor() {
|
||||||
if (
|
if (
|
||||||
!addon.data.templateEditor.window ||
|
!addon.data.template.editor.window ||
|
||||||
Components.utils.isDeadWrapper(addon.data.templateEditor.window) ||
|
Components.utils.isDeadWrapper(addon.data.template.editor.window) ||
|
||||||
addon.data.templateEditor.window.closed
|
addon.data.template.editor.window.closed
|
||||||
) {
|
) {
|
||||||
const windowArgs = {
|
const windowArgs = {
|
||||||
_initPromise: Zotero.Promise.defer(),
|
_initPromise: Zotero.Promise.defer(),
|
||||||
|
|
@ -20,10 +20,10 @@ export async function showTemplateEditor() {
|
||||||
`chrome,centerscreen,resizable,status,width=600,height=400,dialog=no`,
|
`chrome,centerscreen,resizable,status,width=600,height=400,dialog=no`,
|
||||||
windowArgs,
|
windowArgs,
|
||||||
)!;
|
)!;
|
||||||
addon.data.templateEditor.window = _window;
|
addon.data.template.editor.window = _window;
|
||||||
await windowArgs._initPromise.promise;
|
await windowArgs._initPromise.promise;
|
||||||
updateData();
|
updateData();
|
||||||
addon.data.templateEditor.tableHelper = new ztoolkit.VirtualizedTable(
|
addon.data.template.editor.tableHelper = new ztoolkit.VirtualizedTable(
|
||||||
_window!,
|
_window!,
|
||||||
)
|
)
|
||||||
.setContainerId("table-container")
|
.setContainerId("table-container")
|
||||||
|
|
@ -47,14 +47,10 @@ export async function showTemplateEditor() {
|
||||||
staticColumns: true,
|
staticColumns: true,
|
||||||
disableFontSizeScaling: true,
|
disableFontSizeScaling: true,
|
||||||
})
|
})
|
||||||
.setProp("getRowCount", () => addon.data.templateEditor.templates.length)
|
.setProp("getRowCount", () => addon.data.template.editor.templates.length)
|
||||||
.setProp(
|
.setProp("getRowData", (index) => ({
|
||||||
"getRowData",
|
name: addon.data.template.editor.templates[index] || "no data",
|
||||||
(index) =>
|
}))
|
||||||
(addon.data.templateEditor.templates[index] as { name: string }) || {
|
|
||||||
name: "no data",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.setProp("onSelectionChange", (selection) => {
|
.setProp("onSelectionChange", (selection) => {
|
||||||
updateEditor();
|
updateEditor();
|
||||||
updatePreview();
|
updatePreview();
|
||||||
|
|
@ -128,7 +124,7 @@ export async function showTemplateEditor() {
|
||||||
?.addEventListener("click", (ev) => {
|
?.addEventListener("click", (ev) => {
|
||||||
restoreTemplates(_window);
|
restoreTemplates(_window);
|
||||||
});
|
});
|
||||||
addon.data.templateEditor.window?.focus();
|
addon.data.template.editor.window?.focus();
|
||||||
const editorWin = (_window.document.querySelector("#editor") as any)
|
const editorWin = (_window.document.querySelector("#editor") as any)
|
||||||
.contentWindow;
|
.contentWindow;
|
||||||
await waitUtilAsync(() => editorWin?.loadMonaco);
|
await waitUtilAsync(() => editorWin?.loadMonaco);
|
||||||
|
|
@ -136,7 +132,7 @@ export async function showTemplateEditor() {
|
||||||
language: "javascript",
|
language: "javascript",
|
||||||
theme: "vs-light",
|
theme: "vs-light",
|
||||||
});
|
});
|
||||||
addon.data.templateEditor.editor = editor;
|
addon.data.template.editor.editor = editor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -148,33 +144,33 @@ async function refresh() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateData() {
|
function updateData() {
|
||||||
addon.data.templateEditor.templates = addon.api.template.getTemplateKeys();
|
addon.data.template.editor.templates = addon.api.template.getTemplateKeys();
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateTable(selectId?: number) {
|
function updateTable(selectId?: number) {
|
||||||
addon.data.templateEditor.tableHelper?.render(selectId);
|
addon.data.template.editor.tableHelper?.render(selectId);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateEditor() {
|
function updateEditor() {
|
||||||
const name = getSelectedTemplateName();
|
const name = getSelectedTemplateName();
|
||||||
const templateText = addon.api.template.getTemplateText(name);
|
const templateText = addon.api.template.getTemplateText(name);
|
||||||
|
|
||||||
const header = addon.data.templateEditor.window?.document.getElementById(
|
const header = addon.data.template.editor.window?.document.getElementById(
|
||||||
"editor-name",
|
"editor-name",
|
||||||
) as HTMLInputElement;
|
) as HTMLInputElement;
|
||||||
const editor = addon.data.templateEditor.window?.document.getElementById(
|
const editor = addon.data.template.editor.window?.document.getElementById(
|
||||||
"editor",
|
"editor",
|
||||||
) as HTMLIFrameElement;
|
) as HTMLIFrameElement;
|
||||||
const saveTemplate =
|
const saveTemplate =
|
||||||
addon.data.templateEditor.window?.document.getElementById(
|
addon.data.template.editor.window?.document.getElementById(
|
||||||
"save",
|
"save",
|
||||||
) as XUL.Button;
|
) as XUL.Button;
|
||||||
const deleteTemplate =
|
const deleteTemplate =
|
||||||
addon.data.templateEditor.window?.document.getElementById(
|
addon.data.template.editor.window?.document.getElementById(
|
||||||
"delete",
|
"delete",
|
||||||
) as XUL.Button;
|
) as XUL.Button;
|
||||||
const resetTemplate =
|
const resetTemplate =
|
||||||
addon.data.templateEditor.window?.document.getElementById(
|
addon.data.template.editor.window?.document.getElementById(
|
||||||
"reset",
|
"reset",
|
||||||
) as XUL.Button;
|
) as XUL.Button;
|
||||||
if (!name) {
|
if (!name) {
|
||||||
|
|
@ -197,7 +193,7 @@ function updateEditor() {
|
||||||
deleteTemplate.hidden = true;
|
deleteTemplate.hidden = true;
|
||||||
resetTemplate.hidden = false;
|
resetTemplate.hidden = false;
|
||||||
}
|
}
|
||||||
addon.data.templateEditor.editor.setValue(templateText);
|
addon.data.template.editor.editor.setValue(templateText);
|
||||||
editor.hidden = false;
|
editor.hidden = false;
|
||||||
saveTemplate.removeAttribute("disabled");
|
saveTemplate.removeAttribute("disabled");
|
||||||
deleteTemplate.removeAttribute("disabled");
|
deleteTemplate.removeAttribute("disabled");
|
||||||
|
|
@ -211,7 +207,7 @@ async function updatePreview() {
|
||||||
.replace(/<br>/g, "<br/>")
|
.replace(/<br>/g, "<br/>")
|
||||||
.replace(/<hr>/g, "<hr/>")
|
.replace(/<hr>/g, "<hr/>")
|
||||||
.replace(/<img([^>]+)>/g, "<img$1/>");
|
.replace(/<img([^>]+)>/g, "<img$1/>");
|
||||||
const win = addon.data.templateEditor.window;
|
const win = addon.data.template.editor.window;
|
||||||
const container = win?.document.getElementById("preview-container");
|
const container = win?.document.getElementById("preview-container");
|
||||||
if (container) {
|
if (container) {
|
||||||
container.innerHTML = html;
|
container.innerHTML = html;
|
||||||
|
|
@ -219,13 +215,13 @@ async function updatePreview() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSelectedTemplateName() {
|
function getSelectedTemplateName() {
|
||||||
const selectedTemplate = addon.data.templateEditor.templates.find(
|
const selectedTemplate = addon.data.template.editor.templates.find(
|
||||||
(v, i) =>
|
(v, i) =>
|
||||||
addon.data.templateEditor.tableHelper?.treeInstance.selection.isSelected(
|
addon.data.template.editor.tableHelper?.treeInstance.selection.isSelected(
|
||||||
i,
|
i,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
return selectedTemplate?.name || "";
|
return selectedTemplate || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
function createTemplate() {
|
function createTemplate() {
|
||||||
|
|
@ -255,7 +251,7 @@ async function importNoteTemplate() {
|
||||||
|
|
||||||
function saveSelectedTemplate() {
|
function saveSelectedTemplate() {
|
||||||
const name = getSelectedTemplateName();
|
const name = getSelectedTemplateName();
|
||||||
const header = addon.data.templateEditor.window?.document.getElementById(
|
const header = addon.data.template.editor.window?.document.getElementById(
|
||||||
"editor-name",
|
"editor-name",
|
||||||
) as HTMLInputElement;
|
) as HTMLInputElement;
|
||||||
|
|
||||||
|
|
@ -271,7 +267,7 @@ function saveSelectedTemplate() {
|
||||||
|
|
||||||
const template = {
|
const template = {
|
||||||
name: header.value,
|
name: header.value,
|
||||||
text: addon.data.templateEditor.editor.getValue(),
|
text: addon.data.template.editor.editor.getValue(),
|
||||||
};
|
};
|
||||||
addon.api.template.setTemplate(template);
|
addon.api.template.setTemplate(template);
|
||||||
if (name !== template.name) {
|
if (name !== template.name) {
|
||||||
|
|
@ -279,7 +275,7 @@ function saveSelectedTemplate() {
|
||||||
}
|
}
|
||||||
showHint(`Template ${template.name} saved.`);
|
showHint(`Template ${template.name} saved.`);
|
||||||
const selectedId =
|
const selectedId =
|
||||||
addon.data.templateEditor.tableHelper?.treeInstance.selection.selected
|
addon.data.template.editor.tableHelper?.treeInstance.selection.selected
|
||||||
.values()
|
.values()
|
||||||
.next().value;
|
.next().value;
|
||||||
refresh().then(() => updateTable(selectedId));
|
refresh().then(() => updateTable(selectedId));
|
||||||
|
|
@ -300,7 +296,7 @@ function deleteSelectedTemplate() {
|
||||||
function resetSelectedTemplate() {
|
function resetSelectedTemplate() {
|
||||||
const name = getSelectedTemplateName();
|
const name = getSelectedTemplateName();
|
||||||
if (addon.api.template.SYSTEM_TEMPLATE_NAMES.includes(name)) {
|
if (addon.api.template.SYSTEM_TEMPLATE_NAMES.includes(name)) {
|
||||||
addon.data.templateEditor.editor.setValue(
|
addon.data.template.editor.editor.setValue(
|
||||||
addon.api.template.DEFAULT_TEMPLATES.find((t) => t.name === name)?.text ||
|
addon.api.template.DEFAULT_TEMPLATES.find((t) => t.name === name)?.text ||
|
||||||
"",
|
"",
|
||||||
);
|
);
|
||||||
|
|
@ -343,7 +339,7 @@ async function backupTemplates() {
|
||||||
if (!filepath) {
|
if (!filepath) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const keys = addon.api.template.getTemplateKeys().map((t) => t.name);
|
const keys = addon.api.template.getTemplateKeys();
|
||||||
const templates = keys.map((key) => {
|
const templates = keys.map((key) => {
|
||||||
return {
|
return {
|
||||||
name: key,
|
name: key,
|
||||||
|
|
@ -368,7 +364,7 @@ async function restoreTemplates(win: Window) {
|
||||||
}
|
}
|
||||||
const yaml = (await Zotero.File.getContentsAsync(filepath)) as string;
|
const yaml = (await Zotero.File.getContentsAsync(filepath)) as string;
|
||||||
const templates = YAML.parse(yaml) as NoteTemplate[];
|
const templates = YAML.parse(yaml) as NoteTemplate[];
|
||||||
const existingNames = addon.api.template.getTemplateKeys().map((t) => t.name);
|
const existingNames = addon.api.template.getTemplateKeys();
|
||||||
|
|
||||||
for (const t of templates) {
|
for (const t of templates) {
|
||||||
if (existingNames.includes(t.name)) {
|
if (existingNames.includes(t.name)) {
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,12 @@ function showTemplatePicker(
|
||||||
function showTemplatePicker(mode: "export", data?: Record<string, never>): void;
|
function showTemplatePicker(mode: "export", data?: Record<string, never>): void;
|
||||||
function showTemplatePicker(): void;
|
function showTemplatePicker(): void;
|
||||||
function showTemplatePicker(
|
function showTemplatePicker(
|
||||||
mode: typeof addon.data.templatePicker.mode = "insert",
|
mode: typeof addon.data.template.picker.mode = "insert",
|
||||||
data: Record<string, any> = {},
|
data: Record<string, any> = {},
|
||||||
) {
|
) {
|
||||||
if (addon.data.prompt) {
|
if (addon.data.prompt) {
|
||||||
addon.data.templatePicker.mode = mode;
|
addon.data.template.picker.mode = mode;
|
||||||
addon.data.templatePicker.data = data;
|
addon.data.template.picker.data = data;
|
||||||
addon.data.prompt.promptNode.style.display = "flex";
|
addon.data.prompt.promptNode.style.display = "flex";
|
||||||
addon.data.prompt.showCommands(
|
addon.data.prompt.showCommands(
|
||||||
addon.data.prompt.commands.filter(
|
addon.data.prompt.commands.filter(
|
||||||
|
|
@ -32,18 +32,18 @@ function showTemplatePicker(
|
||||||
|
|
||||||
function updateTemplatePicker() {
|
function updateTemplatePicker() {
|
||||||
ztoolkit.Prompt.unregisterAll();
|
ztoolkit.Prompt.unregisterAll();
|
||||||
const templates = addon.api.template.getTemplateKeys();
|
const templateKeys = addon.api.template.getTemplateKeys();
|
||||||
ztoolkit.Prompt.register(
|
ztoolkit.Prompt.register(
|
||||||
templates
|
templateKeys
|
||||||
.filter(
|
.filter(
|
||||||
(template) =>
|
(template) =>
|
||||||
!addon.api.template.SYSTEM_TEMPLATE_NAMES.includes(template.name),
|
!addon.api.template.SYSTEM_TEMPLATE_NAMES.includes(template),
|
||||||
)
|
)
|
||||||
.map((template) => {
|
.map((template) => {
|
||||||
return {
|
return {
|
||||||
name: `Template: ${template.name}`,
|
name: `Template: ${template}`,
|
||||||
label: "BNotes Template",
|
label: "BNotes Template",
|
||||||
callback: getTemplatePromptHandler(template.name),
|
callback: getTemplatePromptHandler(template),
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
@ -54,7 +54,7 @@ function getTemplatePromptHandler(name: string) {
|
||||||
ztoolkit.log(prompt, name);
|
ztoolkit.log(prompt, name);
|
||||||
prompt.promptNode.style.display = "none";
|
prompt.promptNode.style.display = "none";
|
||||||
// TODO: add preview when command is selected
|
// TODO: add preview when command is selected
|
||||||
switch (addon.data.templatePicker.mode) {
|
switch (addon.data.template.picker.mode) {
|
||||||
case "create":
|
case "create":
|
||||||
await createTemplateNoteCallback(name);
|
await createTemplateNoteCallback(name);
|
||||||
break;
|
break;
|
||||||
|
|
@ -66,14 +66,14 @@ function getTemplatePromptHandler(name: string) {
|
||||||
await insertTemplateCallback(name);
|
await insertTemplateCallback(name);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
addon.data.templatePicker.mode = "insert";
|
addon.data.template.picker.mode = "insert";
|
||||||
addon.data.templatePicker.data = {};
|
addon.data.template.picker.data = {};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function insertTemplateCallback(name: string) {
|
async function insertTemplateCallback(name: string) {
|
||||||
const targetNoteItem = Zotero.Items.get(
|
const targetNoteItem = Zotero.Items.get(
|
||||||
addon.data.templatePicker.data.noteId || addon.data.workspace.mainId,
|
addon.data.template.picker.data.noteId || addon.data.workspace.mainId,
|
||||||
);
|
);
|
||||||
let html = "";
|
let html = "";
|
||||||
if (name.toLowerCase().startsWith("[item]")) {
|
if (name.toLowerCase().startsWith("[item]")) {
|
||||||
|
|
@ -88,14 +88,14 @@ async function insertTemplateCallback(name: string) {
|
||||||
await addLineToNote(
|
await addLineToNote(
|
||||||
targetNoteItem,
|
targetNoteItem,
|
||||||
html,
|
html,
|
||||||
addon.data.templatePicker.data.lineIndex,
|
addon.data.template.picker.data.lineIndex,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createTemplateNoteCallback(name: string) {
|
async function createTemplateNoteCallback(name: string) {
|
||||||
addon.data.templatePicker.data.librarySelectedIds =
|
addon.data.template.picker.data.librarySelectedIds =
|
||||||
ZoteroPane.getSelectedItems(true);
|
ZoteroPane.getSelectedItems(true);
|
||||||
switch (addon.data.templatePicker.data.noteType) {
|
switch (addon.data.template.picker.data.noteType) {
|
||||||
case "standalone": {
|
case "standalone": {
|
||||||
const currentCollection = ZoteroPane.getSelectedCollection();
|
const currentCollection = ZoteroPane.getSelectedCollection();
|
||||||
if (!currentCollection) {
|
if (!currentCollection) {
|
||||||
|
|
@ -105,16 +105,16 @@ async function createTemplateNoteCallback(name: string) {
|
||||||
const noteID = await ZoteroPane.newNote();
|
const noteID = await ZoteroPane.newNote();
|
||||||
const noteItem = Zotero.Items.get(noteID);
|
const noteItem = Zotero.Items.get(noteID);
|
||||||
await noteItem.saveTx();
|
await noteItem.saveTx();
|
||||||
addon.data.templatePicker.data.noteId = noteID;
|
addon.data.template.picker.data.noteId = noteID;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "item": {
|
case "item": {
|
||||||
const parentID = addon.data.templatePicker.data.parentItemId;
|
const parentID = addon.data.template.picker.data.parentItemId;
|
||||||
const noteItem = new Zotero.Item("note");
|
const noteItem = new Zotero.Item("note");
|
||||||
noteItem.libraryID = Zotero.Items.get(parentID).libraryID;
|
noteItem.libraryID = Zotero.Items.get(parentID).libraryID;
|
||||||
noteItem.parentID = parentID;
|
noteItem.parentID = parentID;
|
||||||
await noteItem.saveTx();
|
await noteItem.saveTx();
|
||||||
addon.data.templatePicker.data.noteId = noteItem.id;
|
addon.data.template.picker.data.noteId = noteItem.id;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
@ -124,13 +124,13 @@ async function createTemplateNoteCallback(name: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function exportTemplateCallback(name: string) {
|
async function exportTemplateCallback(name: string) {
|
||||||
addon.data.templatePicker.data.librarySelectedIds =
|
addon.data.template.picker.data.librarySelectedIds =
|
||||||
ZoteroPane.getSelectedItems(true);
|
ZoteroPane.getSelectedItems(true);
|
||||||
// Create temp note
|
// Create temp note
|
||||||
const noteItem = new Zotero.Item("note");
|
const noteItem = new Zotero.Item("note");
|
||||||
noteItem.libraryID = Zotero.Libraries.userLibraryID;
|
noteItem.libraryID = Zotero.Libraries.userLibraryID;
|
||||||
await noteItem.saveTx();
|
await noteItem.saveTx();
|
||||||
addon.data.templatePicker.data.noteId = noteItem.id;
|
addon.data.template.picker.data.noteId = noteItem.id;
|
||||||
await insertTemplateCallback(name);
|
await insertTemplateCallback(name);
|
||||||
// Export note
|
// Export note
|
||||||
await addon.hooks.onShowExportNoteOptions([noteItem.id], {
|
await addon.hooks.onShowExportNoteOptions([noteItem.id], {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ import { MenuManager } from "zotero-plugin-toolkit/dist/managers/menu";
|
||||||
import { PromptManager } from "zotero-plugin-toolkit/dist/managers/prompt";
|
import { PromptManager } from "zotero-plugin-toolkit/dist/managers/prompt";
|
||||||
import { ReaderInstanceManager } from "zotero-plugin-toolkit/dist/managers/readerInstance";
|
import { ReaderInstanceManager } from "zotero-plugin-toolkit/dist/managers/readerInstance";
|
||||||
import { ReaderTabPanelManager } from "zotero-plugin-toolkit/dist/managers/readerTabPanel";
|
import { ReaderTabPanelManager } from "zotero-plugin-toolkit/dist/managers/readerTabPanel";
|
||||||
|
import { LargePrefHelper } from "zotero-plugin-toolkit/dist/helpers/largePref";
|
||||||
|
|
||||||
class MyToolkit extends BasicTool {
|
class MyToolkit extends BasicTool {
|
||||||
UI: UITool;
|
UI: UITool;
|
||||||
|
|
@ -54,6 +55,7 @@ class MyToolkit extends BasicTool {
|
||||||
ProgressWindow: typeof ProgressWindowHelper;
|
ProgressWindow: typeof ProgressWindowHelper;
|
||||||
VirtualizedTable: typeof VirtualizedTableHelper;
|
VirtualizedTable: typeof VirtualizedTableHelper;
|
||||||
Dialog: typeof DialogHelper;
|
Dialog: typeof DialogHelper;
|
||||||
|
LargePref: typeof LargePrefHelper;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
@ -69,6 +71,7 @@ class MyToolkit extends BasicTool {
|
||||||
this.ProgressWindow = ProgressWindowHelper;
|
this.ProgressWindow = ProgressWindowHelper;
|
||||||
this.VirtualizedTable = VirtualizedTableHelper;
|
this.VirtualizedTable = VirtualizedTableHelper;
|
||||||
this.Dialog = DialogHelper;
|
this.Dialog = DialogHelper;
|
||||||
|
this.LargePref = LargePrefHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
unregisterAll() {
|
unregisterAll() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue