@type32/vertex-plugin 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,7 @@ import type { PluginManifest, WindowKind } from './manifest';
14
14
  export type { ActiveSession, ActiveWorkspaceFileIndex, UITreeNode };
15
15
  export type { PluginManifest, WindowKind };
16
16
  export type * from './contributions';
17
+ export type * from './components';
17
18
  export type PluginSource = 'core' | 'global' | 'workspace';
18
19
  export type MenuSection = 'app' | 'file' | 'edit' | 'view' | 'help';
19
20
  export interface PluginCommand {
@@ -343,6 +344,21 @@ export interface HostServices {
343
344
  }): Promise<string | null>;
344
345
  save(options?: DialogOptions): Promise<string | null>;
345
346
  };
347
+ /** The app's strings and language. A plugin's own are `plugins.<id>.…`, from its `locales/<locale>.json`. */
348
+ i18n: {
349
+ /** The string for an i18n key in the window's language, `{params}` filled in; the key itself when there's none. Reactive. */
350
+ t(key: string, params?: Record<string, unknown>): string;
351
+ /** The window's language (`en`, `zh-CN`). Reactive. */
352
+ readonly locale: string;
353
+ };
354
+ ui: {
355
+ /** The window's theme as the page draws it. Reactive: it follows light and dark, the colours, a workspace's theme. */
356
+ theme: {
357
+ readonly dark: boolean;
358
+ /** A CSS custom property as the page resolves it now (`--ui-primary` → a colour), for what CSS can't reach (a canvas). */
359
+ value(property: string): string;
360
+ };
361
+ };
346
362
  /**
347
363
  * Trust for programs from a vault (the trusted folders and files, Settings → Trust), so every plugin that runs one
348
364
  * asks the same way. Paths are absolute; `hash` names a program's content (the format's `hash`).
@@ -0,0 +1,107 @@
1
+ /**
2
+ * The props and events of the components `/ui` publishes (`app/plugin/ui.ts`), by the names it exports them under:
3
+ * `<Name>Props`, and `<Name>Emits` for one that emits. Each component declares its props with these, so they can't
4
+ * drift; the SDK's build types each export from them, so `vue-tsc` checks what a plugin passes.
5
+ */
6
+ import type { InternalLinkClickDetail } from '@type32/codemirror-rich-obsidian-editor';
7
+ import type { UiSlot } from './contributions';
8
+ /** One setting: label and description on the left, its control (the default slot) on the right. */
9
+ export interface SettingsRowProps {
10
+ label: string;
11
+ description?: string;
12
+ /** Replaces the description, in the error colour. */
13
+ error?: string;
14
+ /**
15
+ * The setting it edits (`plugins.config.<id>.depth`): it then follows the Settings window's scope (Global only,
16
+ * Changed in this workspace, Use global).
17
+ */
18
+ setting?: string;
19
+ }
20
+ /** A group of `SettingsRow`s under a title, with `actions` beside it. */
21
+ export interface SettingsSectionProps {
22
+ title: string;
23
+ description?: string;
24
+ /** Shows the reset button, which emits `reset`; what "dirty" means is the caller's. */
25
+ dirty?: boolean;
26
+ }
27
+ export interface SettingsSectionEmits {
28
+ reset: [];
29
+ }
30
+ /** A settings page: a sticky header with the title (`header-leading`, `header-trailing`) over the scrolling content. */
31
+ export interface SettingsScrollLayoutProps {
32
+ title?: string;
33
+ /** Read-only while the Settings window edits a workspace: none of the page's settings can be overridden there. */
34
+ globalOnly?: boolean;
35
+ }
36
+ /**
37
+ * A file viewer's frame: the header with the file's name (editable, `v-model:fileName`), the content, the status bar
38
+ * with the registered status items. Slots: `header-left`, `header-right`, the default (with `contentWidthClass`),
39
+ * `status-bar`.
40
+ */
41
+ export interface ViewerLayoutProps {
42
+ /** The file's name without its extension; renaming it emits `update:fileName`. */
43
+ fileName?: string;
44
+ showHeader?: boolean;
45
+ showBreadcrumbs?: boolean;
46
+ renaming?: boolean;
47
+ filePath?: string;
48
+ /** Default `vertical`. */
49
+ scrollMode?: 'vertical' | 'both' | 'none';
50
+ showScrollbar?: boolean;
51
+ showStatusBar?: boolean;
52
+ /** Default `center`. */
53
+ contentAlignment?: 'center' | 'start' | 'stretch';
54
+ /** A Tailwind class; default `max-w-2xl`. */
55
+ contentMaxWidth?: string;
56
+ contentPadding?: boolean;
57
+ cursorText?: boolean;
58
+ /** Room for the header above the content (default `true`); without it the content runs under the header's fade. */
59
+ topSpacing?: boolean;
60
+ bottomSpacing?: boolean;
61
+ ui?: {
62
+ root?: string;
63
+ };
64
+ }
65
+ export interface ViewerLayoutEmits {
66
+ 'update:fileName': [name: string];
67
+ 'on-rename': [oldValue: string, newValue: string];
68
+ }
69
+ /** A Markdown editor inside something else (a card, a form): the note editor's rendering, links and syntax. */
70
+ export interface NoteEditorProps {
71
+ text: string;
72
+ /** The file it's in, for its links and embeds. */
73
+ notePath: string;
74
+ }
75
+ export interface NoteEditorEmits {
76
+ update: [text: string];
77
+ /** Esc. */
78
+ done: [];
79
+ /** An internal link's click, for the caller to follow. */
80
+ openLink: [detail: InternalLinkClickDetail];
81
+ }
82
+ /** Draws a contribution's UI (`component` or `mount`) with `props`, inside an error boundary that names its plugin. */
83
+ export interface PluginMountProps {
84
+ ui: UiSlot<never>;
85
+ props: object;
86
+ }
87
+ /** The vault picker: files or folders of the workspace (`v-model`: workspace-relative paths). */
88
+ export interface VaultPickerProps {
89
+ modelValue?: string[];
90
+ sessionId: string;
91
+ /** Default `any`. */
92
+ kind?: 'file' | 'folder' | 'any';
93
+ multiple?: boolean;
94
+ /** Comma-separated, without dots (`png,jpg`). */
95
+ extensions?: string;
96
+ }
97
+ export interface VaultPickerEmits {
98
+ 'update:modelValue': [paths: string[]];
99
+ }
100
+ /** Two versions of a text, line by line; `header` gets the +/− counts. `sides` missing: still loading. */
101
+ export interface TextDiffProps {
102
+ sides?: {
103
+ before?: string;
104
+ after?: string;
105
+ };
106
+ failed?: boolean;
107
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * A plugin's strings: one file per locale (`en.json`, `zh-CN.json`), each `{"plugins": {"<id>": {…}}}`, the same
3
+ * shape a core plugin's layer has in `i18n/locales/`. A community plugin ships them in `locales/` beside `main.js`;
4
+ * the host merges only `plugins.<id>`, so a plugin's strings can't replace the app's or another plugin's.
5
+ */
6
+ import type { PluginManifest } from './manifest';
7
+ /** A messages object's strings by dotted key (`plugins.graph.view`); a value that isn't an object or a string is skipped. */
8
+ export declare function messageLeaves(messages: unknown, prefix?: string): Map<string, string>;
9
+ /** The i18n keys a manifest names: its settings schema's labels, descriptions and options. */
10
+ export declare function manifestKeys(manifest: Pick<PluginManifest, 'config'>): string[];
11
+ /**
12
+ * What's wrong with plugin `id`'s strings, `files` by locale (`en`, `zh-CN`): each file is an object of strings,
13
+ * all under `plugins.<id>`; `en` is there, and every other locale has exactly its keys, with the same `{params}`;
14
+ * `en` has every key the manifest names. `locales`: the locales there must be a file for (a core plugin's are the
15
+ * app's; a community plugin's others fall back to `en`). Empty when nothing is.
16
+ */
17
+ export declare function checkPluginMessages(id: string, files: Readonly<Record<string, unknown>>, manifest?: Pick<PluginManifest, 'config'>, locales?: readonly string[]): string[];
18
+ /**
19
+ * What the app merges for plugin `id` in each of its `locales`: only `plugins.<id>` of the plugin's files, a locale's
20
+ * strings over `en`'s, so a missing locale or key shows English. A locale with no strings is left out.
21
+ */
22
+ export declare function messagesFor(id: string, files: Readonly<Record<string, unknown>>, locales: readonly string[]): Record<string, Record<string, unknown>>;
@@ -1,12 +1,29 @@
1
- import type { Component, ComputedRef, MaybeRefOrGetter } from 'vue';
2
- export declare const SettingsRow: Component;
3
- export declare const SettingsSection: Component;
4
- export declare const SettingsScrollLayout: Component;
5
- export declare const ViewerLayout: Component;
6
- export declare const NoteEditor: Component;
7
- export declare const PluginMount: Component;
8
- export declare const VaultPicker: Component;
9
- export declare const TextDiff: Component;
1
+ import type { ComputedRef, EmitsToProps, MaybeRefOrGetter } from 'vue';
2
+ import type { NoteEditorEmits, NoteEditorProps, PluginMountProps, SettingsRowProps, SettingsScrollLayoutProps, SettingsSectionEmits, SettingsSectionProps, TextDiffProps, VaultPickerEmits, VaultPickerProps, ViewerLayoutEmits, ViewerLayoutProps } from '#shared/plugin/components';
3
+ export declare const SettingsRow: new () => {
4
+ $props: SettingsRowProps;
5
+ };
6
+ export declare const SettingsSection: new () => {
7
+ $props: SettingsSectionProps & EmitsToProps<SettingsSectionEmits>;
8
+ };
9
+ export declare const SettingsScrollLayout: new () => {
10
+ $props: SettingsScrollLayoutProps;
11
+ };
12
+ export declare const ViewerLayout: new () => {
13
+ $props: ViewerLayoutProps & EmitsToProps<ViewerLayoutEmits>;
14
+ };
15
+ export declare const NoteEditor: new () => {
16
+ $props: NoteEditorProps & EmitsToProps<NoteEditorEmits>;
17
+ };
18
+ export declare const PluginMount: new () => {
19
+ $props: PluginMountProps;
20
+ };
21
+ export declare const VaultPicker: new () => {
22
+ $props: VaultPickerProps & EmitsToProps<VaultPickerEmits>;
23
+ };
24
+ export declare const TextDiff: new () => {
25
+ $props: TextDiffProps;
26
+ };
10
27
  export declare const openExternal: (url: string) => Promise<void>;
11
28
  export declare const useScrollFade: (target: MaybeRefOrGetter<HTMLElement | null | undefined>, options?: {
12
29
  bottom?: boolean | undefined;
package/dist/vite.js CHANGED
@@ -1,4 +1,4 @@
1
- import { readFileSync, readdirSync, writeFileSync } from "node:fs";
1
+ import { existsSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
2
2
  import { join, relative, resolve, sep } from "node:path";
3
3
  import { crc32, deflateRawSync } from "node:zlib";
4
4
  import tailwindcss from "@tailwindcss/vite";
@@ -47,7 +47,7 @@ var KEYS = /* @__PURE__ */ new Set([
47
47
  ]);
48
48
  var isString = (v) => typeof v === "string";
49
49
  var isStringList = (v) => Array.isArray(v) && v.every(isString);
50
- var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
50
+ var isObject$1 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
51
51
  var isNumber = (v) => typeof v === "number" && Number.isFinite(v);
52
52
  var CONFIG_PATH = /^[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)*$/;
53
53
  var FIELD_KEYS = {
@@ -64,7 +64,7 @@ var FIELD_KEYS = {
64
64
  path: ["kind"]
65
65
  };
66
66
  function validateField(f, at, errors) {
67
- if (!isObject(f)) return void errors.push(`${at} must be an object`);
67
+ if (!isObject$1(f)) return void errors.push(`${at} must be an object`);
68
68
  const type = f.type;
69
69
  if (!(type in FIELD_KEYS)) return void errors.push(`${at}.type must be one of ${Object.keys(FIELD_KEYS).join(", ")}`);
70
70
  for (const key of Object.keys(f)) if (![
@@ -95,15 +95,15 @@ function validateField(f, at, errors) {
95
95
  }
96
96
  if (type === "select") {
97
97
  const options = f.options;
98
- if (!Array.isArray(options) || !options.length || !options.every((o) => isObject(o) && isString(o.value) && isString(o.label))) errors.push(`${at}.options must be a non-empty list of {value, label}`);
98
+ if (!Array.isArray(options) || !options.length || !options.every((o) => isObject$1(o) && isString(o.value) && isString(o.label))) errors.push(`${at}.options must be a non-empty list of {value, label}`);
99
99
  else if (!options.some((o) => o.value === d)) errors.push(`${at}.default must be one of its options`);
100
100
  }
101
101
  }
102
102
  function validateConfig(config, errors) {
103
- if (!isObject(config)) return void errors.push("\"config\" must be an object");
103
+ if (!isObject$1(config)) return void errors.push("\"config\" must be an object");
104
104
  for (const key of Object.keys(config)) if (key !== "overridable" && key !== "schema") errors.push(`unknown key "config.${key}"`);
105
105
  if (config.overridable !== void 0) {
106
- if (!isObject(config.overridable)) errors.push("\"config.overridable\" must map paths to \"workspace\" or \"trustedWorkspace\"");
106
+ if (!isObject$1(config.overridable)) errors.push("\"config.overridable\" must map paths to \"workspace\" or \"trustedWorkspace\"");
107
107
  else for (const [path, level] of Object.entries(config.overridable)) {
108
108
  if (!CONFIG_PATH.test(path)) errors.push(`"config.overridable" has an invalid path "${path}"`);
109
109
  if (!OVERRIDE_LEVELS.includes(level)) errors.push(`"config.overridable.${path}" must be "workspace" or "trustedWorkspace"`);
@@ -114,7 +114,7 @@ function validateConfig(config, errors) {
114
114
  const keys = /* @__PURE__ */ new Set();
115
115
  config.schema.forEach((field, i) => {
116
116
  validateField(field, `"config.schema[${i}]"`, errors);
117
- const key = isObject(field) ? field.key : void 0;
117
+ const key = isObject$1(field) ? field.key : void 0;
118
118
  if (keys.has(key)) errors.push(`"config.schema[${i}].key" "${String(key)}" is used twice`);
119
119
  keys.add(key);
120
120
  });
@@ -178,6 +178,61 @@ function validateManifest(raw, appVersion) {
178
178
  manifest: m
179
179
  };
180
180
  }
181
+ // ../../shared/plugin/messages.ts
182
+ var isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
183
+ /** A messages object's strings by dotted key (`plugins.graph.view`); a value that isn't an object or a string is skipped. */
184
+ function messageLeaves(messages, prefix = "") {
185
+ const out = /* @__PURE__ */ new Map();
186
+ if (!isObject(messages)) return out;
187
+ for (const [key, value] of Object.entries(messages)) {
188
+ const at = prefix ? `${prefix}.${key}` : key;
189
+ if (isObject(value)) for (const [k, v] of messageLeaves(value, at)) out.set(k, v);
190
+ else if (typeof value === "string") out.set(at, value);
191
+ }
192
+ return out;
193
+ }
194
+ var params = (text) => [...text.matchAll(/\{(\w+)\}/g)].map((m) => m[1]).sort();
195
+ /** The i18n keys a manifest names: its settings schema's labels, descriptions and options. */
196
+ function manifestKeys(manifest) {
197
+ return (manifest.config?.schema ?? []).flatMap((field) => [
198
+ field.label,
199
+ field.description,
200
+ ...field.type === "select" ? field.options.map((o) => o.label) : []
201
+ ].filter((k) => !!k));
202
+ }
203
+ /**
204
+ * What's wrong with plugin `id`'s strings, `files` by locale (`en`, `zh-CN`): each file is an object of strings,
205
+ * all under `plugins.<id>`; `en` is there, and every other locale has exactly its keys, with the same `{params}`;
206
+ * `en` has every key the manifest names. `locales`: the locales there must be a file for (a core plugin's are the
207
+ * app's; a community plugin's others fall back to `en`). Empty when nothing is.
208
+ */
209
+ function checkPluginMessages(id, files, manifest, locales = []) {
210
+ const errors = [];
211
+ const prefix = `plugins.${id}.`;
212
+ for (const locale of locales) if (!(locale in files)) errors.push(`no ${locale}.json`);
213
+ const leaves = Object.fromEntries(Object.entries(files).map(([locale, messages]) => {
214
+ if (!isObject(messages)) errors.push(`${locale}.json isn't an object`);
215
+ const strings = messageLeaves(messages);
216
+ for (const key of strings.keys()) if (!key.startsWith(prefix)) errors.push(`${locale}.json: "${key}" isn't under "${prefix.slice(0, -1)}"`);
217
+ return [locale, strings];
218
+ }));
219
+ const en = leaves.en;
220
+ if (!en) {
221
+ if (!locales.includes("en") && (Object.keys(files).length || manifest && manifestKeys(manifest).length)) errors.push("no en.json");
222
+ return errors;
223
+ }
224
+ for (const [locale, strings] of Object.entries(leaves)) {
225
+ if (locale === "en") continue;
226
+ for (const key of en.keys()) if (!strings.has(key)) errors.push(`${locale}.json has no "${key}"`);
227
+ for (const key of strings.keys()) if (!en.has(key)) errors.push(`${locale}.json has "${key}", which en.json doesn't`);
228
+ for (const [key, text] of en) {
229
+ const other = strings.get(key);
230
+ if (other !== void 0 && params(other).join() !== params(text).join()) errors.push(`${locale}.json: "${key}" takes other {params} than en.json's`);
231
+ }
232
+ }
233
+ for (const key of manifest ? manifestKeys(manifest) : []) if (!en.has(key)) errors.push(`en.json has no "${key}", which plugin.json names`);
234
+ return errors;
235
+ }
181
236
  // ../../shared/plugin/modules.ts
182
237
  /**
183
238
  * The modules a community plugin imports and gets the host's copy of, so there is one Vue, one CodeMirror, one editor
@@ -190,7 +245,7 @@ var SHARED_GLOBAL = "__vertexModules";
190
245
  /**
191
246
  * The Vite preset a community plugin builds with: `plugins: [vertex()]` in its `vite.config.ts` turns `src/main.ts`
192
247
  * and `plugin.json` into `dist/`, the folder Vertex installs (`plugin.json`, one `main.js`, `styles.css` when there's
193
- * CSS, the files of `public/`); with `pack`, also `plugin.zip` for a release.
248
+ * CSS, the strings of `locales/`, the files of `public/`); with `pack`, also `plugin.zip` for a release.
194
249
  */
195
250
  var PREFIX = "\0vertex-shared:";
196
251
  /**
@@ -296,6 +351,15 @@ function vertex(options = {}) {
296
351
  fileName: "plugin.json",
297
352
  source: `${JSON.stringify(result.manifest, null, 2)}\n`
298
353
  });
354
+ const dir = resolve(root, "locales");
355
+ const files = Object.fromEntries((existsSync(dir) ? readdirSync(dir) : []).filter((f) => f.endsWith(".json")).map((f) => [f.slice(0, -5), JSON.parse(readFileSync(join(dir, f), "utf8"))]));
356
+ const errors = checkPluginMessages(result.manifest.id, files, result.manifest);
357
+ if (errors.length) this.error(`locales/:\n - ${errors.join("\n - ")}`);
358
+ for (const [locale, messages] of Object.entries(files)) this.emitFile({
359
+ type: "asset",
360
+ fileName: `locales/${locale}.json`,
361
+ source: `${JSON.stringify(messages, null, 2)}\n`
362
+ });
299
363
  },
300
364
  closeBundle() {
301
365
  if (!options.pack) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/vertex-plugin",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "private": false,
5
5
  "description": "Vertex SDK - Types, the host's shared modules and a Vite preset for Vertex community plugins.",
6
6
  "type": "module",