@humanonlyweb/writer 0.0.0-stage → 0.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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +355 -2
  3. package/dist/components/WriterContent.d.ts +57 -0
  4. package/dist/components/WriterContent.js +114 -0
  5. package/dist/components/WriterEditor.js +232 -0
  6. package/dist/components/WriterEditor.vue.d.ts +70 -0
  7. package/dist/components/WriterToolbar.js +135 -0
  8. package/dist/components/WriterToolbar.vue.d.ts +27 -0
  9. package/dist/config.d.ts +38 -0
  10. package/dist/config.js +41 -0
  11. package/dist/editor/commands.d.ts +1 -0
  12. package/dist/editor/commands.js +294 -0
  13. package/dist/editor/images.d.ts +1 -0
  14. package/dist/editor/images.js +388 -0
  15. package/dist/editor/live.d.ts +2 -0
  16. package/dist/editor/live.js +410 -0
  17. package/dist/editor/setup.d.ts +9 -0
  18. package/dist/editor/setup.js +466 -0
  19. package/dist/index.d.ts +10 -0
  20. package/dist/index.js +8 -0
  21. package/dist/load-parser.js +12 -0
  22. package/dist/nuxt/module.d.ts +38 -0
  23. package/dist/nuxt/module.js +92 -0
  24. package/dist/nuxt/runtime/plugin.d.ts +2 -0
  25. package/dist/nuxt/runtime/plugin.js +24 -0
  26. package/dist/nuxt/runtime/server/parse.d.ts +12 -0
  27. package/dist/nuxt/runtime/server/parse.js +15 -0
  28. package/dist/package.js +3 -0
  29. package/dist/parse.d.ts +31 -0
  30. package/dist/parse.js +62 -0
  31. package/dist/placeholder.js +5 -0
  32. package/dist/render.d.ts +10 -0
  33. package/dist/render.js +97 -0
  34. package/dist/sanitize.d.ts +11 -0
  35. package/dist/sanitize.js +139 -0
  36. package/dist/stats.d.ts +8 -0
  37. package/dist/stats.js +17 -0
  38. package/dist/styles/core.css +275 -0
  39. package/dist/translations.d.ts +26 -0
  40. package/dist/translations.js +47 -0
  41. package/dist/types.d.ts +9 -0
  42. package/docs/api.md +258 -0
  43. package/package.json +96 -4
@@ -0,0 +1,232 @@
1
+ import { useTranslations, useWriterConfig } from "../config.js";
2
+ import { loadParser } from "../load-parser.js";
3
+ import WriterContent_default from "./WriterContent.js";
4
+ import { computed, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, getCurrentInstance, h, mergeModels, nextTick, onBeforeUnmount, onMounted, openBlock, render, shallowRef, unref, useModel, useTemplateRef, watch } from "vue";
5
+ // src/components/WriterEditor.vue
6
+ const _hoisted_1 = [
7
+ "data-mode",
8
+ "data-ready",
9
+ "data-readonly"
10
+ ];
11
+ const _hoisted_2 = {
12
+ key: 0,
13
+ "data-part": "writer-fallback",
14
+ "aria-busy": "true"
15
+ };
16
+ const _hoisted_3 = [
17
+ "name",
18
+ "value",
19
+ "aria-label",
20
+ "aria-labelledby",
21
+ "aria-describedby",
22
+ "placeholder",
23
+ "readonly"
24
+ ];
25
+ const _hoisted_4 = ["hidden"];
26
+ const _hoisted_5 = ["name", "value"];
27
+ const _sfc_main = /*@__PURE__*/ defineComponent({
28
+ __name: "WriterEditor",
29
+ props: /*@__PURE__*/ mergeModels({
30
+ label: {},
31
+ labelledby: {},
32
+ describedby: {},
33
+ name: {},
34
+ placeholder: {},
35
+ readonly: { type: Boolean },
36
+ mode: {},
37
+ upload: { type: Function },
38
+ links: { type: Function },
39
+ codeLanguages: {},
40
+ extensions: {}
41
+ }, {
42
+ "modelValue": { default: "" },
43
+ "modelModifiers": {}
44
+ }),
45
+ emits: /*@__PURE__*/ mergeModels([
46
+ "focus",
47
+ "blur",
48
+ "uploadError"
49
+ ], ["update:modelValue"]),
50
+ setup(__props, { expose: __expose, emit: __emit }) {
51
+ const model = useModel(__props, "modelValue");
52
+ const props = __props;
53
+ const emit = __emit;
54
+ const t = useTranslations();
55
+ const config = useWriterConfig();
56
+ const appContext = getCurrentInstance()?.appContext ?? null;
57
+ const previews = /* @__PURE__ */ new Map();
58
+ const renderPreview = async (source, host) => {
59
+ let document = previews.get(source);
60
+ if (!document) {
61
+ document = await (await loadParser(config.parser))(source);
62
+ previews.set(source, document);
63
+ if (previews.size > 200) previews.delete(previews.keys().next().value);
64
+ }
65
+ const vnode = h(WriterContent_default, { value: document });
66
+ vnode.appContext = appContext;
67
+ render(vnode, host);
68
+ return () => render(null, host);
69
+ };
70
+ const preview = () => props.mode === "source" ? void 0 : renderPreview;
71
+ const fallback = useTemplateRef("fallback");
72
+ const host = useTemplateRef("host");
73
+ const picker = useTemplateRef("picker");
74
+ const editorCode = typeof window === "undefined" ? void 0 : import("../editor/setup.js");
75
+ const ready = shallowRef(false);
76
+ const active = shallowRef(/* @__PURE__ */ new Set());
77
+ const canUndo = shallowRef(false);
78
+ const uploading = shallowRef(0);
79
+ const canRedo = shallowRef(false);
80
+ let editor;
81
+ let unmounted = false;
82
+ const attributes = computed(() => {
83
+ const result = {
84
+ spellcheck: "true",
85
+ autocapitalize: "sentences"
86
+ };
87
+ if (props.labelledby) result["aria-labelledby"] = props.labelledby;
88
+ else result["aria-label"] = props.label ?? t.value.editor;
89
+ if (props.describedby) result["aria-describedby"] = props.describedby;
90
+ if (props.placeholder) result["aria-placeholder"] = props.placeholder;
91
+ if (props.readonly) result["aria-readonly"] = "true";
92
+ return result;
93
+ });
94
+ onMounted(async () => {
95
+ const { createEditor } = await (editorCode ?? import("../editor/setup.js"));
96
+ if (unmounted || !host.value) return;
97
+ const textarea = fallback.value;
98
+ const focused = !!textarea && document.activeElement === textarea;
99
+ editor = createEditor({
100
+ parent: host.value,
101
+ doc: textarea?.value ?? model.value,
102
+ selection: textarea && focused ? {
103
+ anchor: textarea.selectionStart,
104
+ head: textarea.selectionEnd
105
+ } : void 0,
106
+ attributes: attributes.value,
107
+ placeholder: props.placeholder,
108
+ readonly: props.readonly,
109
+ preview: preview(),
110
+ blocks: Object.entries(config.blocks).map(([name, block]) => ({
111
+ name,
112
+ label: block.label,
113
+ snippet: block.snippet
114
+ })),
115
+ translations: t.value,
116
+ codeLanguages: props.codeLanguages,
117
+ extensions: props.extensions,
118
+ upload: props.upload,
119
+ links: props.links,
120
+ onChange: (doc) => model.value = doc,
121
+ onState: (state) => {
122
+ active.value = state.active;
123
+ canUndo.value = state.undo;
124
+ canRedo.value = state.redo;
125
+ },
126
+ onFocus: (value) => value ? emit("focus") : emit("blur"),
127
+ onUploadError: (error, file) => emit("uploadError", error, file),
128
+ onUploading: (count) => uploading.value = count
129
+ });
130
+ if (editor.getDoc() !== model.value) model.value = editor.getDoc();
131
+ ready.value = true;
132
+ await nextTick();
133
+ editor.view.requestMeasure();
134
+ if (focused) editor.view.focus();
135
+ });
136
+ onBeforeUnmount(() => {
137
+ unmounted = true;
138
+ editor?.destroy();
139
+ });
140
+ watch(model, (value) => editor?.setDoc(value ?? ""));
141
+ watch(() => props.readonly, (value) => editor?.setReadonly(value));
142
+ watch(attributes, (value) => editor?.setAttributes(value));
143
+ watch(() => props.mode, () => editor?.setPreview(preview()));
144
+ function onFallbackInput(event) {
145
+ model.value = event.target.value;
146
+ }
147
+ function onPick(event) {
148
+ const input = event.target;
149
+ if (input.files?.length) editor?.insertFiles([...input.files]);
150
+ input.value = "";
151
+ editor?.view.focus();
152
+ }
153
+ __expose({
154
+ /** True once CodeMirror has replaced the server-rendered fallback. */
155
+ ready,
156
+ /** Formatting at the cursor. */
157
+ active,
158
+ /** Whether files can be inserted (an `upload` is set). */
159
+ canUpload: computed(() => !!props.upload),
160
+ canUndo,
161
+ canRedo,
162
+ /**
163
+ * Uploads in progress. Until they finish, their files are placeholders in the
164
+ * Markdown: wait for 0 before saving.
165
+ */
166
+ uploading,
167
+ /** Applies a command. `image` opens the file picker with uploads, else inserts an empty image. */
168
+ run(command) {
169
+ if (!editor || props.readonly) return false;
170
+ if (command !== "image") return editor.run(command);
171
+ if (props.upload) picker.value?.click();
172
+ else editor.insertImage();
173
+ return true;
174
+ },
175
+ /** Replaces the selection with `text`. */
176
+ insert: (text) => editor?.insert(text),
177
+ /** Uploads and inserts files at the cursor. */
178
+ insertFiles: (files) => editor?.insertFiles(files) ?? false,
179
+ focus: () => editor ? editor.view.focus() : fallback.value?.focus(),
180
+ /** The CodeMirror view, for anything else. */
181
+ view: () => editor?.view
182
+ });
183
+ return (_ctx, _cache) => {
184
+ return openBlock(), createElementBlock("div", {
185
+ "data-part": "writer-editor",
186
+ "data-mode": __props.mode ?? "live",
187
+ "data-ready": ready.value || void 0,
188
+ "data-readonly": __props.readonly || void 0
189
+ }, [
190
+ !ready.value && __props.mode !== "source" ? (openBlock(), createElementBlock("div", _hoisted_2, [createVNode(unref(WriterContent_default), { value: model.value }, null, 8, ["value"])])) : !ready.value ? (openBlock(), createElementBlock("textarea", {
191
+ key: 1,
192
+ ref_key: "fallback",
193
+ ref: fallback,
194
+ "data-part": "writer-fallback",
195
+ name: __props.name,
196
+ value: model.value,
197
+ "aria-label": __props.labelledby ? void 0 : __props.label ?? unref(t).editor,
198
+ "aria-labelledby": __props.labelledby,
199
+ "aria-describedby": __props.describedby,
200
+ placeholder: __props.placeholder,
201
+ readonly: __props.readonly,
202
+ spellcheck: "true",
203
+ onInput: onFallbackInput
204
+ }, null, 40, _hoisted_3)) : createCommentVNode("v-if", true),
205
+ createElementVNode("div", {
206
+ ref_key: "host",
207
+ ref: host,
208
+ "data-part": "writer-host",
209
+ hidden: !ready.value
210
+ }, null, 8, _hoisted_4),
211
+ __props.name && (ready.value || __props.mode !== "source") ? (openBlock(), createElementBlock("input", {
212
+ key: 2,
213
+ type: "hidden",
214
+ name: __props.name,
215
+ value: model.value
216
+ }, null, 8, _hoisted_5)) : createCommentVNode("v-if", true),
217
+ __props.upload ? (openBlock(), createElementBlock("input", {
218
+ key: 3,
219
+ ref_key: "picker",
220
+ ref: picker,
221
+ type: "file",
222
+ accept: "image/*",
223
+ multiple: "",
224
+ hidden: "",
225
+ tabindex: "-1",
226
+ onChange: onPick
227
+ }, null, 544)) : createCommentVNode("v-if", true)
228
+ ], 8, _hoisted_1);
229
+ };
230
+ }
231
+ });
232
+ export { _sfc_main as default };
@@ -0,0 +1,70 @@
1
+ import { CommandName } from "../translations.js";
2
+ import { LinkSuggestion } from "../editor/setup.js";
3
+ import { LanguageDescription } from "@codemirror/language";
4
+ import { Extension } from "@codemirror/state";
5
+ import { EditorView } from "@codemirror/view";
6
+ type __VLS_Props = {
7
+ /** Accessible name. Defaults to the "editor" translation. */
8
+ label?: string;
9
+ /** Id of an element naming the editor, instead of `label`. */
10
+ labelledby?: string;
11
+ /** Id of an element describing the editor, e.g. a hint or an error. */
12
+ describedby?: string;
13
+ /** Form field name: the Markdown is submitted with its form. */
14
+ name?: string;
15
+ placeholder?: string;
16
+ readonly?: boolean;
17
+ /**
18
+ * `live` (default): blocks, images and formatting show as they'll be published, and
19
+ * turn into Markdown where the cursor is. `source`: plain Markdown throughout.
20
+ */
21
+ mode?: "live" | "source";
22
+ /** Uploads a pasted, dropped or picked file and resolves to its URL. Enables file insertion. */
23
+ upload?: (file: File) => Promise<string>;
24
+ /** Suggests link targets as the author types `](`, e.g. the site's pages matching `query`. */
25
+ links?: (query: string) => LinkSuggestion[] | Promise<LinkSuggestion[]>;
26
+ /** Languages for highlighting fenced code, e.g. `languages` from `@codemirror/language-data`. */
27
+ codeLanguages?: readonly LanguageDescription[];
28
+ /** Extra CodeMirror extensions. Read once, on mount. */
29
+ extensions?: Extension;
30
+ };
31
+ type __VLS_ModelProps = {
32
+ modelValue?: string;
33
+ };
34
+ type __VLS_PublicProps = __VLS_Props & __VLS_ModelProps;
35
+ declare const __VLS_export: import("vue").DefineComponent<__VLS_PublicProps, {
36
+ /** True once CodeMirror has replaced the server-rendered fallback. */
37
+ ready: import("vue").ShallowRef<boolean, boolean>;
38
+ /** Formatting at the cursor. */
39
+ active: import("vue").ShallowRef<ReadonlySet<CommandName>, ReadonlySet<CommandName>>;
40
+ /** Whether files can be inserted (an `upload` is set). */
41
+ canUpload: import("vue").ComputedRef<boolean>;
42
+ canUndo: import("vue").ShallowRef<boolean, boolean>;
43
+ canRedo: import("vue").ShallowRef<boolean, boolean>;
44
+ /**
45
+ * Uploads in progress. Until they finish, their files are placeholders in the
46
+ * Markdown: wait for 0 before saving.
47
+ */
48
+ uploading: import("vue").ShallowRef<number, number>;
49
+ /** Applies a command. `image` opens the file picker with uploads, else inserts an empty image. */
50
+ run(command: CommandName): boolean;
51
+ /** Replaces the selection with `text`. */
52
+ insert: (text: string) => void | undefined;
53
+ /** Uploads and inserts files at the cursor. */
54
+ insertFiles: (files: readonly File[]) => boolean;
55
+ focus: () => void;
56
+ /** The CodeMirror view, for anything else. */
57
+ view: () => EditorView | undefined;
58
+ }, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
59
+ blur: () => any;
60
+ focus: () => any;
61
+ "update:modelValue": (value: string) => any;
62
+ uploadError: (error: unknown, file: File) => any;
63
+ }, string, import("vue").PublicProps, Readonly<__VLS_PublicProps> & Readonly<{
64
+ onBlur?: (() => any) | undefined;
65
+ onFocus?: (() => any) | undefined;
66
+ "onUpdate:modelValue"?: ((value: string) => any) | undefined;
67
+ onUploadError?: ((error: unknown, file: File) => any) | undefined;
68
+ }>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
69
+ declare const _default: typeof __VLS_export;
70
+ export { _default as default };
@@ -0,0 +1,135 @@
1
+ import { useTranslations } from "../config.js";
2
+ import { Fragment, computed, createElementBlock, createTextVNode, defineComponent, openBlock, renderList, renderSlot, shallowRef, toDisplayString, unref, useTemplateRef, withModifiers } from "vue";
3
+ // src/components/WriterToolbar.vue
4
+ const _hoisted_1 = ["aria-label"];
5
+ const _hoisted_2 = [
6
+ "data-command",
7
+ "tabindex",
8
+ "aria-label",
9
+ "aria-pressed",
10
+ "aria-disabled",
11
+ "aria-keyshortcuts",
12
+ "title",
13
+ "onClick"
14
+ ];
15
+ const _sfc_main = /*@__PURE__*/ defineComponent({
16
+ __name: "WriterToolbar",
17
+ props: {
18
+ editor: {},
19
+ items: { default: () => [
20
+ "heading2",
21
+ "heading3",
22
+ "bold",
23
+ "italic",
24
+ "link",
25
+ "code",
26
+ "quote",
27
+ "bulletList",
28
+ "orderedList",
29
+ "codeBlock",
30
+ "image"
31
+ ] },
32
+ label: {}
33
+ },
34
+ setup(__props) {
35
+ const t = useTranslations();
36
+ const root = useTemplateRef("root");
37
+ const TOGGLES = /* @__PURE__ */ new Set([
38
+ "bold",
39
+ "italic",
40
+ "strikethrough",
41
+ "code",
42
+ "link",
43
+ "heading1",
44
+ "heading2",
45
+ "heading3",
46
+ "quote",
47
+ "bulletList",
48
+ "orderedList",
49
+ "taskList",
50
+ "codeBlock"
51
+ ]);
52
+ const SHORTCUTS = {
53
+ bold: "B",
54
+ italic: "I",
55
+ link: "K",
56
+ code: "E",
57
+ strikethrough: "Shift+X",
58
+ heading1: "Alt+1",
59
+ heading2: "Alt+2",
60
+ heading3: "Alt+3",
61
+ orderedList: "Shift+7",
62
+ bulletList: "Shift+8",
63
+ taskList: "Shift+9",
64
+ footnote: "Alt+F",
65
+ undo: "Z",
66
+ redo: "Shift+Z"
67
+ };
68
+ const buttons = computed(() => __props.items.map((name) => ({
69
+ name,
70
+ label: t.value.commands[name],
71
+ pressed: TOGGLES.has(name) ? !!__props.editor?.active.has(name) : void 0,
72
+ shortcut: SHORTCUTS[name],
73
+ unavailable: name === "undo" && !__props.editor?.canUndo || name === "redo" && !__props.editor?.canRedo
74
+ })));
75
+ const disabled = computed(() => !__props.editor?.ready);
76
+ function buttonsIn(el) {
77
+ return [...el.querySelectorAll("button")];
78
+ }
79
+ const last = shallowRef();
80
+ const tabStop = computed(() => last.value && __props.items.includes(last.value) ? last.value : __props.items[0]);
81
+ function rove(target) {
82
+ last.value = target.dataset.command;
83
+ target.focus();
84
+ }
85
+ function onKeydown(event) {
86
+ if (!root.value || !(event.target instanceof HTMLButtonElement)) return;
87
+ const list = buttonsIn(root.value);
88
+ const index = list.indexOf(event.target);
89
+ const rtl = getComputedStyle(root.value).direction === "rtl";
90
+ const step = {
91
+ ArrowRight: rtl ? -1 : 1,
92
+ ArrowLeft: rtl ? 1 : -1
93
+ }[event.key];
94
+ const target = event.key === "Home" ? list[0] : event.key === "End" ? list.at(-1) : step ? list[(index + step + list.length) % list.length] : void 0;
95
+ if (!target) return;
96
+ event.preventDefault();
97
+ rove(target);
98
+ }
99
+ function run(name) {
100
+ last.value = name;
101
+ if (!disabled.value) __props.editor?.run(name);
102
+ }
103
+ const mac = (shortcut) => `⌘${shortcut.replace("Alt+", "⌥").replace("Shift+", "⇧")}`;
104
+ return (_ctx, _cache) => {
105
+ return openBlock(), createElementBlock("div", {
106
+ ref_key: "root",
107
+ ref: root,
108
+ role: "toolbar",
109
+ "data-part": "writer-toolbar",
110
+ "aria-label": __props.label ?? unref(t).toolbar,
111
+ onKeydown
112
+ }, [(openBlock(true), createElementBlock(Fragment, null, renderList(buttons.value, (button) => {
113
+ return openBlock(), createElementBlock("button", {
114
+ key: button.name,
115
+ type: "button",
116
+ "data-part": "writer-toolbar-item",
117
+ "data-command": button.name,
118
+ tabindex: button.name === tabStop.value ? 0 : -1,
119
+ "aria-label": button.label,
120
+ "aria-pressed": button.pressed,
121
+ "aria-disabled": disabled.value || button.unavailable || void 0,
122
+ "aria-keyshortcuts": button.shortcut && `Control+${button.shortcut} Meta+${button.shortcut}`,
123
+ title: button.shortcut ? `${button.label} (Ctrl+${button.shortcut} / ${mac(button.shortcut)})` : void 0,
124
+ onMousedown: _cache[0] || (_cache[0] = withModifiers(() => {}, ["prevent"])),
125
+ onClick: ($event) => run(button.name)
126
+ }, [renderSlot(_ctx.$slots, "item", {
127
+ command: button.name,
128
+ label: button.label,
129
+ active: !!button.pressed
130
+ }, () => [createTextVNode(toDisplayString(button.label), 1)])], 40, _hoisted_2);
131
+ }), 128))], 40, _hoisted_1);
132
+ };
133
+ }
134
+ });
135
+ export { _sfc_main as default };
@@ -0,0 +1,27 @@
1
+ import { CommandName } from "../translations.js";
2
+ import { ToolbarEditor } from "../types.js";
3
+ type __VLS_Props = {
4
+ /** The editor to format. */
5
+ editor: ToolbarEditor | null | undefined;
6
+ /** Commands to show, in order. */
7
+ items?: readonly CommandName[];
8
+ /** Accessible name. Defaults to the "toolbar" translation. */
9
+ label?: string;
10
+ };
11
+ type __VLS_Slots = {
12
+ /** A button's content, e.g. an icon. Defaults to its label, which stays its accessible name. */
13
+ item?(props: {
14
+ command: CommandName;
15
+ label: string;
16
+ active: boolean;
17
+ }): unknown;
18
+ };
19
+ declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
20
+ declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
21
+ declare const _default: typeof __VLS_export;
22
+ type __VLS_WithSlots<T, S> = T & {
23
+ new (): {
24
+ $slots: S;
25
+ };
26
+ };
27
+ export { _default as default };
@@ -0,0 +1,38 @@
1
+ import { WriterTranslations, WriterTranslationsInput } from "./translations.js";
2
+ import { ParserOptions } from "./parse.js";
3
+ import { App, Component, ComputedRef, InjectionKey, Ref } from "vue";
4
+ export interface WriterBlock {
5
+ /** Rendered for `::name` in content. Receives the attributes as props and the body as slots. */
6
+ component: Component;
7
+ /** Slash menu entry. Defaults to the block name. */
8
+ label?: string;
9
+ /**
10
+ * Inserted from the slash menu, in CodeMirror snippet syntax: `${}` marks where the
11
+ * cursor goes, `${name}` a field Tab moves to. Defaults to an empty block.
12
+ */
13
+ snippet?: string;
14
+ }
15
+ export interface WriterOptions {
16
+ /** Components authors can use, keyed by their Markdown name: `{ callout }` for `::callout`. */
17
+ blocks?: Record<string, Component | WriterBlock>;
18
+ /** Replace how native elements render, e.g. `{ a: AppLink, img: AppImage }`. */
19
+ components?: Record<string, Component>;
20
+ /** Used by `<WriterContent>` for Markdown strings. Parse stored documents with the same options. */
21
+ parser?: ParserOptions;
22
+ /** The components' built-in text. Pass only what you change; a ref keeps it live. */
23
+ translations?: WriterTranslationsInput | Ref<WriterTranslationsInput>;
24
+ }
25
+ export interface WriterConfig {
26
+ blocks: Readonly<Record<string, Required<WriterBlock>>>;
27
+ components: Readonly<Record<string, Component>>;
28
+ parser: ParserOptions;
29
+ translations: Ref<WriterTranslationsInput>;
30
+ }
31
+ export declare const WRITER_CONFIG_KEY: InjectionKey<WriterConfig>;
32
+ /** `app.use(createWriter({ blocks }))`. Scoped to the app, so nothing leaks between SSR requests. */
33
+ export declare function createWriter(options?: WriterOptions): {
34
+ install(app: App): void;
35
+ };
36
+ export declare function useWriterConfig(): WriterConfig;
37
+ /** The built-in text: English, with the app's `translations` on top. */
38
+ export declare function useTranslations(): ComputedRef<WriterTranslations>;
package/dist/config.js ADDED
@@ -0,0 +1,41 @@
1
+ import { resolveTranslations } from "./translations.js";
2
+ import { kebabCase } from "comark/utils";
3
+ import { computed, inject, isRef, shallowRef } from "vue";
4
+ // src/config.ts
5
+ const WRITER_CONFIG_KEY = Symbol("humanonlyweb-writer");
6
+ const isBlock = (value) => typeof value === "object" && value !== null && "component" in value;
7
+ function resolveConfig(options = {}) {
8
+ const blocks = {};
9
+ for (const [key, value] of Object.entries(options.blocks ?? {})) {
10
+ const name = kebabCase(key);
11
+ const block = isBlock(value) ? value : { component: value };
12
+ blocks[name] = {
13
+ component: block.component,
14
+ label: block.label ?? name,
15
+ snippet: block.snippet ?? `::${name}\n\${}\n::`
16
+ };
17
+ }
18
+ const { translations = {} } = options;
19
+ return {
20
+ blocks,
21
+ components: options.components ?? {},
22
+ parser: options.parser ?? {},
23
+ translations: isRef(translations) ? translations : shallowRef(translations)
24
+ };
25
+ }
26
+ const DEFAULT_CONFIG = resolveConfig();
27
+ /** `app.use(createWriter({ blocks }))`. Scoped to the app, so nothing leaks between SSR requests. */
28
+ function createWriter(options = {}) {
29
+ return { install(app) {
30
+ app.provide(WRITER_CONFIG_KEY, resolveConfig(options));
31
+ } };
32
+ }
33
+ function useWriterConfig() {
34
+ return inject(WRITER_CONFIG_KEY, DEFAULT_CONFIG);
35
+ }
36
+ /** The built-in text: English, with the app's `translations` on top. */
37
+ function useTranslations() {
38
+ const { translations } = useWriterConfig();
39
+ return computed(() => resolveTranslations(translations.value));
40
+ }
41
+ export { WRITER_CONFIG_KEY, createWriter, useTranslations, useWriterConfig };
@@ -0,0 +1 @@
1
+ import { EditorState } from "@codemirror/state";