Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(script): missing some core fields from FNote (closes #10129)
Elian Doran520ca081fe88
diff --git a/packages/codemirror/src/type_completion/index.spec.ts b/packages/codemirror/src/type_completion/index.spec.tsindex ce502a0f9b..1a359e8500 100644--- a/packages/codemirror/src/type_completion/index.spec.ts+++ b/packages/codemirror/src/type_completion/index.spec.ts@@ -233,6 +233,44 @@ describe("script note diagnostics", () => {}, TIMEOUT);});+describe("frontend note (ScriptFNote) surface", () => {+ it("offers the expanded note member set on api.currentNote", async () => {+ const names = await completionsAtMarker(SCRIPT_MIME_FRONTEND, "api.currentNote.|");+ // A representative slice across the families added to ScriptFNote:+ // attribute accessors, type predicates, hierarchy, content, fields.+ for (const member of [+ "getOwnedLabelValue", "getRelation", "hasRelation", "getAttributeValue",+ "isHtml", "isJavaScript", "isFolder", "isArchived",+ "getSubtreeNoteIds", "hasChildren", "getChildBranches",+ "getJsonContent", "getAttachments", "getMetadata", "targetRelations"+ ]) {+ expect(names).toContain(member);+ }+ }, TIMEOUT);++ it("type-checks the expanded note members (real signatures, not any)", async () => {+ const codes = await getScriptDiagnosticCodes(+ SCRIPT_MIME_FRONTEND,+ "const note = api.currentNote;\n"+ + "if (note.isHtml() && !note.isArchived) {\n"+ + " const v = note.getLabelValue('foo');\n"+ + " const branches = note.getChildBranches();\n"+ + " const ids = await note.getSubtreeNoteIds(true);\n"+ + " api.showMessage((v ?? '') + branches.length + ids.length);\n"+ + "}"+ );+ expect(codes).toEqual([]);+ }, TIMEOUT);++ it("still flags an unknown note member (curated subset, not any)", async () => {+ const codes = await getScriptDiagnosticCodes(+ SCRIPT_MIME_FRONTEND,+ "const v = api.currentNote.thisNoteMemberDoesNotExist();"+ );+ expect(codes).toContain(TS_UNKNOWN_PROPERTY);+ }, TIMEOUT);+});+describe("backend custom request handler api (req/res/pathParams)", () => {const HANDLER = { customRequestHandler: true };diff --git a/packages/commons/src/lib/script_api.ts b/packages/commons/src/lib/script_api.tsindex be023f2c97..5e788fb82d 100644--- a/packages/commons/src/lib/script_api.ts+++ b/packages/commons/src/lib/script_api.ts@@ -26,6 +26,25 @@ export interface ScriptAttribute {isOwned: boolean;}+/** An attachment as seen by frontend scripts (subset of the client's `FAttachment`). */+export interface ScriptAttachment {+ attachmentId: string;+ ownerId: string;+ role: string;+ mime: string;+ title: string;+ utcDateModified: string;+ contentLength: number;+}++/** Creation/modification timestamps returned by {@link ScriptFNote.getMetadata}. */+export interface ScriptNoteMetadata {+ dateCreated: string;+ utcDateCreated: string;+ dateModified: string;+ utcDateModified: string;+}+/** A note as seen by frontend scripts (subset of the client's `FNote`). */export interface ScriptFNote {noteId: string;@@ -33,29 +52,113 @@ export interface ScriptFNote {type: string;mime: string;isProtected: boolean;+ /** Whether the note is archived (has the `archived` label). */+ isArchived: boolean;+ /** Whether the note's metadata (title, attributes) is read-only. */+ isMetadataReadOnly: boolean;+ /** IDs of this note's owned attributes. */attributes: string[];+ /** IDs of relations pointing *at* this note. */+ targetRelations: string[];+ /** Parent note IDs. */parents: string[];+ /** Child note IDs. */children: string[];+ // --- Hierarchy ---------------------------------------------------------getParentNotes(): ScriptFNote[];getChildNotes(): Promise<ScriptFNote[]>;getParentNoteIds(): string[];getChildNoteIds(): string[];+ getChildNoteIdsWithArchiveFiltering(includeArchived?: boolean): Promise<string[]>;+ getSubtreeNotes(): Promise<ScriptFNote[]>;+ getSubtreeNoteIds(includeArchived?: boolean): Promise<string[]>;+ hasChildren(): boolean;+ hasAncestor(ancestorNoteId: string, followTemplates?: boolean): boolean;+ isRoot(): boolean;+ getParentBranchIds(): string[];+ getBranchIds(): string[];+ getParentBranches(): ScriptFBranch[];+ getChildBranches(): ScriptFBranch[];+ getAllNotePaths(): string[][];+ getBestNotePath(hoistedNoteId?: string, activeNotePath?: string | null): string[];+ getBestNotePathString(hoistedNoteId?: string): string;+ // --- Attributes (inherited + own) --------------------------------------getAttributes(type?: string, name?: string): ScriptAttribute[];getOwnedAttributes(type?: string, name?: string): ScriptAttribute[];getAttribute(type: string, name: string): ScriptAttribute | null;+ getOwnedAttribute(type: string, name: string): ScriptAttribute | null;+ getAttributeValue(type: string, name: string): string | null;+ getOwnedAttributeValue(type: string, name: string): string | null;hasAttribute(type: string, name: string): boolean;+ hasOwnedAttribute(type: string, name: string): boolean;+ getAttributeDefinitions(): ScriptAttribute[];+ getPromotedDefinitionAttributes(): ScriptAttribute[];++ // --- Labels ------------------------------------------------------------getLabels(name?: string): ScriptAttribute[];+ getOwnedLabels(name?: string): ScriptAttribute[];+ getLabel(name: string): ScriptAttribute | null;+ getOwnedLabel(name: string): ScriptAttribute | null;getLabelValue(name: string): string | null;+ getOwnedLabelValue(name: string): string | null;hasLabel(name: string): boolean;+ hasOwnedLabel(name: string): boolean;+ hasLabelOrDisabled(name: string): boolean;+ isLabelTruthy(name: string): boolean;++ // --- Relations ---------------------------------------------------------getRelations(name?: string): ScriptAttribute[];+ getOwnedRelations(name: string): ScriptAttribute[];+ getRelation(name: string): ScriptAttribute | null;+ getOwnedRelation(name: string): ScriptAttribute | null;getRelationValue(name: string): string | null;+ getOwnedRelationValue(name: string): string | null;+ hasRelation(name: string): boolean;+ hasOwnedRelation(name: string): boolean;+ getLabelOrRelation(nameWithPrefix: string): string | null;getRelationTarget(name: string): Promise<ScriptFNote | null>;+ getRelationTargets(name: string): Promise<(ScriptFNote | null)[]>;+ getTargetRelations(): ScriptAttribute[];+ getTargetRelationSourceNotes(): Promise<ScriptFNote[]>;+ // --- Content & attachments ---------------------------------------------getContent(): Promise<string | Uint8Array>;+ getJsonContent(): Promise<unknown>;+ getMetadata(): Promise<ScriptNoteMetadata>;+ attachments: ScriptAttachment[] | null;+ getAttachments(): Promise<ScriptAttachment[]>;+ getAttachmentsByRole(role: string): Promise<ScriptAttachment[]>;+ getAttachmentById(attachmentId: string): Promise<ScriptAttachment | undefined>;++ // --- Type & state predicates -------------------------------------------getIcon(): string;- isRoot(): boolean;+ isFolder(): boolean;+ isShared(): boolean;+ isContentAvailable(): boolean;+ isJson(): boolean;+ isJavaScript(): boolean;+ isJsx(): boolean;+ isHtml(): boolean;+ isMarkdown(): boolean;+ isOptions(): boolean;+ isTriliumScript(): boolean;+ isTriliumSqlite(): boolean;+}++/** A branch (parent↔child link) as seen by frontend scripts (subset of `FBranch`). */+export interface ScriptFBranch {+ branchId: string;+ noteId: string;+ parentNoteId: string;+ notePosition: number;+ prefix?: string;+ isExpanded?: boolean;+ getNote(): Promise<ScriptFNote | null>;+ getNoteFromCache(): ScriptFNote | undefined;+ getParentNote(): Promise<ScriptFNote | null>;+ isTopLevel(): boolean;}/** A split/tab context as seen by frontend scripts (subset of `NoteContext`). */