mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 11:02:28 +08:00
chore(client/ts): port services/bundle
This commit is contained in:
parent
c956d4358c
commit
c0e9684f73
@ -4,9 +4,21 @@ import toastService from "./toast.js";
|
|||||||
import froca from "./froca.js";
|
import froca from "./froca.js";
|
||||||
import utils from "./utils.js";
|
import utils from "./utils.js";
|
||||||
import { t } from "./i18n.js";
|
import { t } from "./i18n.js";
|
||||||
|
import { Entity } from "./frontend_script_api.js";
|
||||||
|
|
||||||
async function getAndExecuteBundle(noteId, originEntity = null, script = null, params = null) {
|
// TODO: Deduplicate with server.
|
||||||
const bundle = await server.post(`script/bundle/${noteId}`, {
|
interface Bundle {
|
||||||
|
script: string;
|
||||||
|
noteId: string;
|
||||||
|
allNoteIds: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Widget {
|
||||||
|
parentWidget?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getAndExecuteBundle(noteId: string, originEntity = null, script = null, params = null) {
|
||||||
|
const bundle = await server.post<Bundle>(`script/bundle/${noteId}`, {
|
||||||
script,
|
script,
|
||||||
params
|
params
|
||||||
});
|
});
|
||||||
@ -14,24 +26,23 @@ async function getAndExecuteBundle(noteId, originEntity = null, script = null, p
|
|||||||
return await executeBundle(bundle, originEntity);
|
return await executeBundle(bundle, originEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeBundle(bundle, originEntity, $container) {
|
async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery<HTMLElement>) {
|
||||||
const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container);
|
const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await (function () {
|
return await (function () {
|
||||||
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
|
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
|
||||||
}.call(apiContext));
|
}.call(apiContext));
|
||||||
}
|
} catch (e: any) {
|
||||||
catch (e) {
|
|
||||||
const note = await froca.getNote(bundle.noteId);
|
const note = await froca.getNote(bundle.noteId);
|
||||||
|
|
||||||
toastService.showAndLogError(`Execution of JS note "${note.title}" with ID ${bundle.noteId} failed with error: ${e.message}`);
|
toastService.showAndLogError(`Execution of JS note "${note?.title}" with ID ${bundle.noteId} failed with error: ${e?.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeStartupBundles() {
|
async function executeStartupBundles() {
|
||||||
const isMobile = utils.isMobile();
|
const isMobile = utils.isMobile();
|
||||||
const scriptBundles = await server.get("script/startup" + (isMobile ? "?mobile=true" : ""));
|
const scriptBundles = await server.get<Bundle[]>("script/startup" + (isMobile ? "?mobile=true" : ""));
|
||||||
|
|
||||||
for (const bundle of scriptBundles) {
|
for (const bundle of scriptBundles) {
|
||||||
await executeBundle(bundle);
|
await executeBundle(bundle);
|
||||||
@ -39,11 +50,14 @@ async function executeStartupBundles() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class WidgetsByParent {
|
class WidgetsByParent {
|
||||||
|
|
||||||
|
private byParent: Record<string, Widget[]>;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.byParent = {};
|
this.byParent = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
add(widget) {
|
add(widget: Widget) {
|
||||||
if (!widget.parentWidget) {
|
if (!widget.parentWidget) {
|
||||||
console.log(`Custom widget does not have mandatory 'parentWidget' property defined`);
|
console.log(`Custom widget does not have mandatory 'parentWidget' property defined`);
|
||||||
return;
|
return;
|
||||||
@ -53,7 +67,7 @@ class WidgetsByParent {
|
|||||||
this.byParent[widget.parentWidget].push(widget);
|
this.byParent[widget.parentWidget].push(widget);
|
||||||
}
|
}
|
||||||
|
|
||||||
get(parentName) {
|
get(parentName: string) {
|
||||||
if (!this.byParent[parentName]) {
|
if (!this.byParent[parentName]) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@ -62,12 +76,12 @@ class WidgetsByParent {
|
|||||||
// previously, custom widgets were provided as a single instance, but that has the disadvantage
|
// previously, custom widgets were provided as a single instance, but that has the disadvantage
|
||||||
// for splits where we actually need multiple instaces and thus having a class to instantiate is better
|
// for splits where we actually need multiple instaces and thus having a class to instantiate is better
|
||||||
// https://github.com/zadam/trilium/issues/4274
|
// https://github.com/zadam/trilium/issues/4274
|
||||||
.map(w => w.prototype ? new w() : w);
|
.map((w: any) => w.prototype ? new w() : w);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getWidgetBundlesByParent() {
|
async function getWidgetBundlesByParent() {
|
||||||
const scriptBundles = await server.get("script/widgets");
|
const scriptBundles = await server.get<Bundle[]>("script/widgets");
|
||||||
|
|
||||||
const widgetsByParent = new WidgetsByParent();
|
const widgetsByParent = new WidgetsByParent();
|
||||||
|
|
||||||
@ -80,7 +94,7 @@ async function getWidgetBundlesByParent() {
|
|||||||
widget._noteId = bundle.noteId;
|
widget._noteId = bundle.noteId;
|
||||||
widgetsByParent.add(widget);
|
widgetsByParent.add(widget);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
const noteId = bundle.noteId;
|
const noteId = bundle.noteId;
|
||||||
const note = await froca.getNote(noteId);
|
const note = await froca.getNote(noteId);
|
||||||
toastService.showPersistent({
|
toastService.showPersistent({
|
||||||
@ -88,7 +102,7 @@ async function getWidgetBundlesByParent() {
|
|||||||
icon: "alert",
|
icon: "alert",
|
||||||
message: t("toast.bundle-error.message", {
|
message: t("toast.bundle-error.message", {
|
||||||
id: noteId,
|
id: noteId,
|
||||||
title: note.title,
|
title: note?.title,
|
||||||
message: e.message
|
message: e.message
|
||||||
})
|
})
|
||||||
});
|
});
|
@ -53,7 +53,7 @@ interface ExecResult {
|
|||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Entity {
|
export interface Entity {
|
||||||
noteId: string;
|
noteId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import FrontendScriptApi from './frontend_script_api.js';
|
import FrontendScriptApi, { Entity } from './frontend_script_api.js';
|
||||||
import utils from './utils.js';
|
import utils from './utils.js';
|
||||||
import froca from './froca.js';
|
import froca from './froca.js';
|
||||||
|
|
||||||
async function ScriptContext(startNoteId: string, allNoteIds: string[], originEntity = null, $container: JQuery<HTMLElement> | null = null) {
|
async function ScriptContext(startNoteId: string, allNoteIds: string[], originEntity: Entity | null = null, $container: JQuery<HTMLElement> | null = null) {
|
||||||
const modules: Record<string, { exports: unknown }> = {};
|
const modules: Record<string, { exports: unknown }> = {};
|
||||||
|
|
||||||
await froca.initializedPromise;
|
await froca.initializedPromise;
|
||||||
|
2
src/public/app/types.d.ts
vendored
2
src/public/app/types.d.ts
vendored
@ -82,7 +82,7 @@ declare global {
|
|||||||
setNote(noteId: string);
|
setNote(noteId: string);
|
||||||
}
|
}
|
||||||
|
|
||||||
var logError: (message: string) => void;
|
var logError: (message: string, e?: Error) => void;
|
||||||
var logInfo: (message: string) => void;
|
var logInfo: (message: string) => void;
|
||||||
var glob: CustomGlobals;
|
var glob: CustomGlobals;
|
||||||
var require: RequireMethod;
|
var require: RequireMethod;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user