mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-01 04:32:26 +08:00
chore(client/ts): port services/script_context
This commit is contained in:
parent
650a116193
commit
7c2002c589
@ -445,10 +445,8 @@ interface Api {
|
|||||||
/**
|
/**
|
||||||
* <p>This is the main frontend API interface for scripts. All the properties and methods are published in the "api" object
|
* <p>This is the main frontend API interface for scripts. All the properties and methods are published in the "api" object
|
||||||
* available in the JS frontend notes. You can use e.g. <code>api.showMessage(api.startNote.title);</code></p>
|
* available in the JS frontend notes. You can use e.g. <code>api.showMessage(api.startNote.title);</code></p>
|
||||||
*
|
|
||||||
* @constructor
|
|
||||||
*/
|
*/
|
||||||
function FrontendScriptApi (this: Api, startNote: FNote, currentNote: FNote, originEntity: Entity | null = null, $container = null) {
|
function FrontendScriptApi(this: Api, startNote: FNote, currentNote: FNote, originEntity: Entity | null = null, $container: JQuery<HTMLElement> | null = null) {
|
||||||
|
|
||||||
this.$container = $container;
|
this.$container = $container;
|
||||||
this.startNote = startNote;
|
this.startNote = startNote;
|
||||||
@ -671,4 +669,6 @@ function FrontendScriptApi (this: Api, startNote: FNote, currentNote: FNote, ori
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default FrontendScriptApi;
|
export default FrontendScriptApi as any as {
|
||||||
|
new (startNote: FNote, currentNote: FNote, originEntity: Entity | null, $container: JQuery<HTMLElement> | null): Api
|
||||||
|
};
|
||||||
|
@ -2,20 +2,24 @@ import FrontendScriptApi 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, allNoteIds, originEntity = null, $container = null) {
|
async function ScriptContext(startNoteId: string, allNoteIds: string[], originEntity = null, $container: JQuery<HTMLElement> | null = null) {
|
||||||
const modules = {};
|
const modules: Record<string, { exports: unknown }> = {};
|
||||||
|
|
||||||
await froca.initializedPromise;
|
await froca.initializedPromise;
|
||||||
|
|
||||||
const startNote = await froca.getNote(startNoteId);
|
const startNote = await froca.getNote(startNoteId);
|
||||||
const allNotes = await froca.getNotes(allNoteIds);
|
const allNotes = await froca.getNotes(allNoteIds);
|
||||||
|
|
||||||
|
if (!startNote) {
|
||||||
|
throw new Error(`Could not find start note ${startNoteId}.`);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
modules: modules,
|
modules: modules,
|
||||||
notes: utils.toObject(allNotes, note => [note.noteId, note]),
|
notes: utils.toObject(allNotes, note => [note.noteId, note]),
|
||||||
apis: utils.toObject(allNotes, note => [note.noteId, new FrontendScriptApi(startNote, note, originEntity, $container)]),
|
apis: utils.toObject(allNotes, note => [note.noteId, new FrontendScriptApi(startNote, note, originEntity, $container)]),
|
||||||
require: moduleNoteIds => {
|
require: (moduleNoteIds: string) => {
|
||||||
return moduleName => {
|
return (moduleName: string) => {
|
||||||
const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
|
const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
|
||||||
const note = candidates.find(c => c.title === moduleName);
|
const note = candidates.find(c => c.title === moduleName);
|
||||||
|
|
@ -138,8 +138,8 @@ function formatSize(size: number) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function toObject<T>(array: T[], fn: (arg0: T) => [key: string, value: T]) {
|
function toObject<T, R>(array: T[], fn: (arg0: T) => [key: string, value: R]) {
|
||||||
const obj: Record<string, T> = {};
|
const obj: Record<string, R> = {};
|
||||||
|
|
||||||
for (const item of array) {
|
for (const item of array) {
|
||||||
const [key, value] = fn(item);
|
const [key, value] = fn(item);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user