@humanonlyweb/writer 0.1.0 → 0.2.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.
@@ -123,6 +123,8 @@ async function finish(view, file, token, options, previous) {
123
123
  if (local) setTimeout(() => URL.revokeObjectURL(local), 3e4);
124
124
  }
125
125
  }
126
+ /** The newlines to add to `newlines` for a blank line. */
127
+ const gap = (newlines) => "\n".repeat(Math.max(0, 2 - newlines));
126
128
  /**
127
129
  * Uploads `files`, inserted at `at`, each on its own line, in one edit; or uploads the
128
130
  * first into the image line `replace`. Placeholders go in at once and the uploads run
@@ -149,9 +151,11 @@ async function uploadFiles(view, files, options, target) {
149
151
  else {
150
152
  const { state } = view;
151
153
  const at = Math.min("at" in target ? target.at : 0, state.doc.length);
152
- const before = at > 0 && state.sliceDoc(at - 1, at) !== "\n" ? "\n" : "";
153
- const after = state.sliceDoc(at, at + 1) === "\n" ? "" : "\n";
154
- const insert = before + lines.join("\n") + after;
154
+ const textBefore = state.sliceDoc(0, at);
155
+ const textAfter = state.sliceDoc(at);
156
+ const before = textBefore.trim() ? gap(/\n*$/.exec(textBefore)?.[0].length ?? 0) : "";
157
+ const after = textAfter.trim() ? gap(/^\n*/.exec(textAfter)?.[0].length ?? 0) : textAfter.startsWith("\n") ? "" : "\n";
158
+ const insert = before + lines.join("\n\n") + after;
155
159
  view.dispatch({
156
160
  changes: {
157
161
  from: at,
@@ -163,6 +167,18 @@ async function uploadFiles(view, files, options, target) {
163
167
  }
164
168
  await Promise.all(uploads.map(({ file, token }) => finish(view, file, token, options, previous)));
165
169
  }
170
+ /** A card button: its words, or an icon named by them (`data-icon`, for styling). */
171
+ function action(label, icon) {
172
+ const button = element("button", { type: "button" });
173
+ if (!icon) {
174
+ button.textContent = label;
175
+ return button;
176
+ }
177
+ button.setAttribute("aria-label", label);
178
+ button.dataset.icon = "";
179
+ button.append(icon());
180
+ return button;
181
+ }
166
182
  function element(tag, props = {}, ...children) {
167
183
  const { dataPart, ...rest } = props;
168
184
  const node = Object.assign(document.createElement(tag), rest);
@@ -221,7 +237,7 @@ var ImageCardWidget = class extends WidgetType {
221
237
  /** Back to writing, on the line after the image. */
222
238
  leave(view, dom) {
223
239
  const { to } = this.range(view, dom);
224
- if (to === view.state.doc.length) view.dispatch({
240
+ if (to === view.state.doc.length && !view.state.readOnly) view.dispatch({
225
241
  changes: {
226
242
  from: to,
227
243
  insert: "\n"
@@ -250,14 +266,8 @@ var ImageCardWidget = class extends WidgetType {
250
266
  textContent: t.imageUploading
251
267
  });
252
268
  status.setAttribute("role", "status");
253
- const replace = element("button", {
254
- type: "button",
255
- textContent: t.imageReplace
256
- });
257
- const remove = element("button", {
258
- type: "button",
259
- textContent: t.imageRemove
260
- });
269
+ const replace = action(t.imageReplace, this.options.icons?.replace);
270
+ const remove = action(t.imageRemove, this.options.icons?.remove);
261
271
  const frame = element("div", { className: "cm-md-image-frame" }, image, empty, status, element("div", { className: "cm-md-image-actions" }, ...upload ? [replace] : [], remove));
262
272
  const caption = element("input", {
263
273
  type: "text",
@@ -281,14 +291,21 @@ var ImageCardWidget = class extends WidgetType {
281
291
  caption,
282
292
  alt,
283
293
  url
284
- ]) input.addEventListener("keydown", (event) => {
285
- if (event.key === "Enter" || event.key === "Escape") {
286
- event.preventDefault();
287
- this.leave(view, root);
288
- }
289
- });
294
+ ]) {
295
+ input.addEventListener("beforeinput", (event) => {
296
+ if (view.state.readOnly) event.preventDefault();
297
+ });
298
+ input.addEventListener("keydown", (event) => {
299
+ if (event.key === "Enter" || event.key === "Escape") {
300
+ event.preventDefault();
301
+ this.leave(view, root);
302
+ }
303
+ });
304
+ }
290
305
  if (upload) {
291
- const choosePicked = () => pickFile((file) => upload(view, file, this.range(view, root)));
306
+ const choosePicked = () => {
307
+ if (!view.state.readOnly) pickFile((file) => upload(view, file, this.range(view, root)));
308
+ };
292
309
  choose.addEventListener("click", choosePicked);
293
310
  replace.addEventListener("click", choosePicked);
294
311
  frame.addEventListener("dragover", (event) => {
@@ -303,10 +320,11 @@ var ImageCardWidget = class extends WidgetType {
303
320
  if (!file?.type.startsWith("image/")) return;
304
321
  event.preventDefault();
305
322
  event.stopPropagation();
306
- upload(view, file, this.range(view, root));
323
+ if (!view.state.readOnly) upload(view, file, this.range(view, root));
307
324
  });
308
325
  }
309
326
  remove.addEventListener("click", () => {
327
+ if (view.state.readOnly) return;
310
328
  const { from, to } = this.range(view, root);
311
329
  view.dispatch({
312
330
  changes: {
@@ -309,6 +309,7 @@ function cardKeys(field) {
309
309
  };
310
310
  const removeOrSelect = (direction) => (view) => {
311
311
  const { state } = view;
312
+ if (state.readOnly) return false;
312
313
  const selected = selectedImage(state);
313
314
  if (selected) {
314
315
  const to = Math.min(selected.to + 1, state.doc.length);
@@ -347,7 +348,7 @@ function cardKeys(field) {
347
348
  {
348
349
  key: "Enter",
349
350
  run: (view) => {
350
- const selected = selectedImage(view.state);
351
+ const selected = !view.state.readOnly && selectedImage(view.state);
351
352
  if (!selected) return false;
352
353
  view.dispatch({
353
354
  changes: {
@@ -1,4 +1,5 @@
1
1
  import "./commands.js";
2
+ import "./images.js";
2
3
  import "./live.js";
3
4
  import { LanguageDescription } from "@codemirror/language";
4
5
  import { Extension } from "@codemirror/state";
@@ -128,6 +128,16 @@ const theme = EditorView.theme({
128
128
  }
129
129
  });
130
130
  const URL_PATTERN = /^https?:\/\/\S+$/;
131
+ /** Slash menu items that start a line rather than a block of their own. */
132
+ const LINE_STARTS = /* @__PURE__ */ new Set([
133
+ "heading1",
134
+ "heading2",
135
+ "heading3",
136
+ "bulletList",
137
+ "orderedList",
138
+ "taskList",
139
+ "quote"
140
+ ]);
131
141
  const snapshot = (state) => ({
132
142
  active: activeCommands(state),
133
143
  undo: undoDepth(state) > 0,
@@ -167,6 +177,7 @@ function createEditor(options) {
167
177
  const liveOptions = (render) => ({
168
178
  render,
169
179
  translations: t,
180
+ icons: options.imageIcons,
170
181
  uploadInto: uploadOptions ? (target, file, range) => void uploadFiles(target, [file], uploadOptions, { replace: range }) : void 0
171
182
  });
172
183
  const slashItems = [
@@ -240,7 +251,15 @@ function createEditor(options) {
240
251
  type: "text",
241
252
  apply: (view, completion, start, end) => {
242
253
  const command = item.name === "footnote" ? commands.footnote : item.name === "image" ? imageCommand : void 0;
243
- if (!command) return snippet(item.snippet)(view, completion, start - 1, end);
254
+ if (!command) {
255
+ const { doc } = view.state;
256
+ const here = doc.lineAt(start);
257
+ const filled = (n) => n >= 1 && n <= doc.lines && !!doc.line(n).text.trim();
258
+ const block = !LINE_STARTS.has(item.name);
259
+ const before = block && filled(here.number - 1) ? "\n" : "";
260
+ const after = block && filled(here.number + 1) ? "\n" : "";
261
+ return snippet(before + item.snippet + after)(view, completion, start - 1, end);
262
+ }
244
263
  const removeSlash = view.state.update({ changes: {
245
264
  from: start - 1,
246
265
  to: end
@@ -360,6 +379,7 @@ function createEditor(options) {
360
379
  ]),
361
380
  EditorView.domEventHandlers({
362
381
  paste(event, view) {
382
+ if (view.state.readOnly) return false;
363
383
  const files = [...event.clipboardData?.files ?? []];
364
384
  if (files.length && insertFiles(files)) {
365
385
  event.preventDefault();
package/dist/index.d.ts CHANGED
@@ -3,8 +3,9 @@ import { CommandName, WriterTranslations, WriterTranslationsInput, defaultTransl
3
3
  import _default$1 from "./components/WriterEditor.vue.js";
4
4
  import { ToolbarEditor } from "./types.js";
5
5
  import _default$2 from "./components/WriterToolbar.vue.js";
6
+ import { ParserOptions } from "./parser-options.js";
6
7
  import { countWords, readingMinutes } from "./stats.js";
7
- import { MarkdownDocument, ParserOptions } from "./parse.js";
8
+ import { MarkdownDocument } from "./parse.js";
8
9
  import { WRITER_CONFIG_KEY, WriterBlock, WriterConfig, WriterOptions, createWriter, useTranslations, useWriterConfig } from "./config.js";
9
10
  import { RenderOptions, renderDocument } from "./render.js";
10
11
  export { type CommandName, type MarkdownDocument, type ParserOptions, type RenderOptions, type ToolbarEditor, WRITER_CONFIG_KEY, type WriterBlock, type WriterConfig, _default as WriterContent, _default$1 as WriterEditor, type WriterOptions, _default$2 as WriterToolbar, type WriterTranslations, type WriterTranslationsInput, countWords, createWriter, defaultTranslations, readingMinutes, renderDocument, useTranslations, useWriterConfig };
@@ -1,5 +1,5 @@
1
1
  import { WriterTranslationsInput } from "../translations.js";
2
- import { ParserOptions } from "../parse.js";
2
+ import { ParserOptions } from "../parser-options.js";
3
3
  /** Auto-registered components. A test keeps it in step with `src/index.ts`. */
4
4
  export declare const COMPONENTS: readonly ["WriterContent", "WriterEditor", "WriterToolbar"];
5
5
  export interface ModuleOptions {
@@ -30,9 +30,6 @@ declare module "@nuxt/schema" {
30
30
  interface AppConfigInput {
31
31
  writer?: WriterAppConfig;
32
32
  }
33
- interface AppConfig {
34
- writer?: WriterAppConfig;
35
- }
36
33
  }
37
34
  declare const _default: import("@nuxt/schema").NuxtModule<ModuleOptions, ModuleOptions, false>;
38
35
  export { _default as default };
@@ -1,7 +1,7 @@
1
1
  import { version } from "../package.js";
2
2
  import { kebabCase } from "comark/utils";
3
3
  import { createHash } from "node:crypto";
4
- import { addComponent, addPlugin, addServerImports, addTemplate, createResolver, defineNuxtModule, getLayerDirectories, resolveFiles, updateTemplates } from "@nuxt/kit";
4
+ import { addComponent, addPlugin, addServerImports, addTemplate, addTypeTemplate, createResolver, defineNuxtModule, getLayerDirectories, resolveFiles, updateTemplates } from "@nuxt/kit";
5
5
  // src/nuxt/module.ts
6
6
  const PACKAGE = "@humanonlyweb/writer";
7
7
  const TEMPLATE = "humanonlyweb-writer.mjs";
@@ -55,8 +55,11 @@ var module_default = defineNuxtModule({
55
55
  return [
56
56
  `import { defineAsyncComponent } from "vue";`,
57
57
  ...elements.length ? [`import { ${[...new Set(elements.map(([, name]) => name))].join(", ")} } from "#components";`] : [],
58
+ `/** @type {Record<string, import("vue").Component>} */`,
58
59
  `export const components = {${elements.map(([tag, name]) => ` ${JSON.stringify(tag)}: ${name},`).join("")} };`,
60
+ `/** @type {Record<string, import("vue").Component>} */`,
59
61
  `export const blocks = {\n${entries.join("\n")}\n};`,
62
+ `/** @type {import("${PACKAGE}/parse").ParserOptions} */`,
60
63
  `export const parser = ${JSON.stringify(options.parser)};`
61
64
  ].join("\n");
62
65
  }
@@ -73,6 +76,25 @@ var module_default = defineNuxtModule({
73
76
  const version$1 = `${version}+${hash.slice(0, 8)}`;
74
77
  config.virtual["#humanonlyweb-writer/parser"] = [`export default ${JSON.stringify(options.parser)};`, `export const version = ${JSON.stringify(version$1)};`].join("\n");
75
78
  });
79
+ addTypeTemplate({
80
+ filename: "types/humanonlyweb-writer-build.d.ts",
81
+ getContents: () => `declare module "#build/humanonlyweb-writer.mjs" {
82
+ export const blocks: Record<string, import("vue").Component>;
83
+ export const components: Record<string, import("vue").Component>;
84
+ export const parser: import("${PACKAGE}/parse").ParserOptions;
85
+ }`
86
+ });
87
+ addTypeTemplate({
88
+ filename: "types/humanonlyweb-writer.d.ts",
89
+ getContents: () => `declare module "#humanonlyweb-writer/parser" {
90
+ const options: Omit<import("${PACKAGE}/parse").ParserOptions, "plugins">;
91
+ export default options;
92
+ export const version: string;
93
+ }`
94
+ }, {
95
+ nitro: true,
96
+ nuxt: true
97
+ });
76
98
  addServerImports([
77
99
  {
78
100
  name: "parseWriterDocument",
@@ -9,7 +9,8 @@ var plugin_default = defineNuxtPlugin({
9
9
  const appConfig = useAppConfig();
10
10
  const meta = appConfig.writer?.blocks ?? {};
11
11
  const withMeta = {};
12
- for (const [name, component] of Object.entries(blocks)) withMeta[name] = {
12
+ const found = blocks;
13
+ for (const [name, component] of Object.entries(found)) withMeta[name] = {
13
14
  component,
14
15
  ...meta[name]
15
16
  };
package/dist/package.js CHANGED
@@ -1,3 +1,3 @@
1
1
  // package.json
2
- var version = "0.1.0";
2
+ var version = "0.2.0";
3
3
  export { version };
package/dist/parse.d.ts CHANGED
@@ -1,24 +1,9 @@
1
+ import { ParserOptions } from "./parser-options.js";
1
2
  import { NATIVE_TAGS, isSafeUrl, sanitize } from "./sanitize.js";
2
3
  import { countWords, readingMinutes } from "./stats.js";
3
- import { ComarkPlugin, MarkdownDocument, MarkdownDocument as MarkdownDocument$1 } from "comark";
4
- import { FootnotesConfig } from "comark/plugins/footnotes";
5
- import { Toc, Toc as Toc$1, TocLink } from "comark/plugins/toc";
6
- import { RangiOptions, RangiOptions as RangiOptions$1 } from "comark/plugins/rangi";
7
- export interface ParserOptions {
8
- /** `[^1]` footnotes. Pass an object to change the section label or back link. */
9
- footnotes?: boolean | FootnotesConfig;
10
- /** Put a table of contents in `document.meta.toc`. Pass `{ depth }` for deeper headings. */
11
- toc?: boolean | Partial<Toc$1>;
12
- /** Turn URL-like text into links. */
13
- linkify?: boolean;
14
- /**
15
- * Highlight fenced code with rangi (light, no WASM). Pass options for themes or line
16
- * spans. Its code loads only when a document has code.
17
- */
18
- highlight?: boolean | RangiOptions$1;
19
- /** Extra Comark plugins, e.g. `comark/plugins/shiki`. They run before sanitizing. */
20
- plugins?: ComarkPlugin[];
21
- }
4
+ import { MarkdownDocument, MarkdownDocument as MarkdownDocument$1 } from "comark";
5
+ import { Toc, TocLink } from "comark/plugins/toc";
6
+ import { RangiOptions } from "comark/plugins/rangi";
22
7
  export type Parse = (markdown: string) => Promise<MarkdownDocument$1>;
23
8
  /**
24
9
  * A parser for stored Markdown. Raw HTML is text, never markup. Run it on the server
@@ -28,4 +13,4 @@ export type Parse = (markdown: string) => Promise<MarkdownDocument$1>;
28
13
  export declare function createParser(options?: ParserOptions): Parse;
29
14
  /** `createParser()` with the defaults. */
30
15
  export declare const parse: Parse;
31
- export { type MarkdownDocument, NATIVE_TAGS, type RangiOptions, type Toc, type TocLink, countWords, isSafeUrl, readingMinutes, sanitize };
16
+ export { type MarkdownDocument, NATIVE_TAGS, type ParserOptions, type RangiOptions, type Toc, type TocLink, countWords, isSafeUrl, readingMinutes, sanitize };
@@ -0,0 +1,19 @@
1
+ import { ComarkPlugin } from "comark";
2
+ import { FootnotesConfig } from "comark/plugins/footnotes";
3
+ import { Toc } from "comark/plugins/toc";
4
+ import { RangiOptions } from "comark/plugins/rangi";
5
+ export interface ParserOptions {
6
+ /** `[^1]` footnotes. Pass an object to change the section label or back link. */
7
+ footnotes?: boolean | FootnotesConfig;
8
+ /** Put a table of contents in `document.meta.toc`. Pass `{ depth }` for deeper headings. */
9
+ toc?: boolean | Partial<Toc>;
10
+ /** Turn URL-like text into links. */
11
+ linkify?: boolean;
12
+ /**
13
+ * Highlight fenced code with rangi (light, no WASM). Pass options for themes or line
14
+ * spans. Its code loads only when a document has code.
15
+ */
16
+ highlight?: boolean | RangiOptions;
17
+ /** Extra Comark plugins, e.g. `comark/plugins/shiki`. They run before sanitizing. */
18
+ plugins?: ComarkPlugin[];
19
+ }
@@ -215,6 +215,13 @@
215
215
  transition: opacity 0.15s;
216
216
  }
217
217
 
218
+ /* An icon in place of the words (the `image-replace` and `image-remove` slots): centred, no line box around it. */
219
+ .cm-md-image-actions button[data-icon] {
220
+ display: inline-grid;
221
+ place-items: center;
222
+ line-height: 0;
223
+ }
224
+
218
225
  .cm-md-image:is(:hover, :focus-within, [data-selected]) .cm-md-image-actions {
219
226
  opacity: 1;
220
227
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@humanonlyweb/writer",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Markdown block editor and renderer for Vue and Nuxt: CodeMirror editing, Comark rendering, SSR-safe.",
5
5
  "keywords": [
6
6
  "a11y",
@@ -23,8 +23,7 @@
23
23
  "url": "git+https://github.com/humanonlyweb/writer.git"
24
24
  },
25
25
  "files": [
26
- "dist",
27
- "docs"
26
+ "dist"
28
27
  ],
29
28
  "type": "module",
30
29
  "sideEffects": [
@@ -41,13 +40,16 @@
41
40
  "access": "public"
42
41
  },
43
42
  "scripts": {
44
- "build": "vp pack",
43
+ "build": "vp pack && nuxt build docs && node scripts/package-docs.mjs",
45
44
  "dev": "vp pack --watch",
46
45
  "test": "vp test",
47
- "check": "nuxt prepare playground && vp check",
46
+ "check": "nuxt prepare docs && vp check",
48
47
  "typecheck": "vue-tsc --noEmit",
49
- "playground": "nuxt dev playground",
50
- "playground:build": "nuxt build playground",
48
+ "docs": "nuxt dev docs",
49
+ "docs:build": "nuxt build docs",
50
+ "docs:preview": "nuxt build docs && wrangler dev --config docs/wrangler.jsonc",
51
+ "docs:deploy": "nuxt build docs && wrangler deploy --config docs/wrangler.jsonc",
52
+ "docs:typecheck": "nuxt typecheck docs",
51
53
  "prepublishOnly": "vp run build",
52
54
  "prepare": "vp config --no-agent"
53
55
  },
@@ -60,18 +62,20 @@
60
62
  "@codemirror/view": "^6.43.15",
61
63
  "@lezer/highlight": "^1.2.5",
62
64
  "@lezer/markdown": "^1.8.1",
63
- "comark": "^0.8.1",
65
+ "comark": "^0.9.0",
64
66
  "rangi": "^2.2.0"
65
67
  },
66
68
  "devDependencies": {
67
- "@nuxt/kit": "^4.6.2",
68
- "@nuxt/schema": "^4.6.2",
69
+ "@humanonlyweb/ui": "^0.2.2",
70
+ "@nuxt/fonts": "1.0.1",
71
+ "@nuxt/kit": "^4.6.4",
72
+ "@nuxt/schema": "^4.6.4",
69
73
  "@types/node": "26.6.5",
70
74
  "@vitejs/plugin-vue": "6.0.10",
71
75
  "@vitest/browser-playwright": "5.0.5",
72
76
  "@vue/test-utils": "2.5.1",
73
77
  "axe-core": "4.14.0",
74
- "nuxt": "^4.6.2",
78
+ "nuxt": "^4.6.4",
75
79
  "playwright": "1.64.1",
76
80
  "typescript": "npm:typescript-native-bridge@6.0.3-bridge.19.tsgo.7.0.2",
77
81
  "unplugin-vue": "8.0.0",
@@ -79,7 +83,8 @@
79
83
  "vite-plus": "1.4.0",
80
84
  "vue": "3.5.44",
81
85
  "vue-router": "5.4.0",
82
- "vue-tsc": "3.3.13"
86
+ "vue-tsc": "3.3.13",
87
+ "wrangler": "4.151.1"
83
88
  },
84
89
  "peerDependencies": {
85
90
  "@nuxt/kit": "^4.0.0",