@writeon/core 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.
@@ -1,4 +1,4 @@
1
- import { t as Command } from "./types-Dy7Bm7XX.js";
1
+ import { t as Command } from "./types-BwODQ2T0.js";
2
2
  /** Toggle bold on the selection. Requires the `bold` extension. */
3
3
  export declare const toggleBold: () => Command;
4
4
  /** Toggle italic on the selection. Requires the `italic` extension. */
@@ -6,7 +6,7 @@
6
6
  * @module @writeon/core/extension
7
7
  */
8
8
  import { MarkType, Node, Schema } from "prosemirror-model";
9
- import { c as Extension, f as NodeAttribute, l as ExtensionContext, n as DocJSON, t as Command, u as ExtensionSpec } from "./types-Dy7Bm7XX.js";
9
+ import { c as Extension, f as NodeAttribute, l as ExtensionContext, n as DocJSON, t as Command, u as ExtensionSpec } from "./types-BwODQ2T0.js";
10
10
  import "prosemirror-state";
11
11
  import { InputRule } from "prosemirror-inputrules";
12
12
  /**
@@ -1,4 +1,4 @@
1
- import { c as Extension } from "./types-Dy7Bm7XX.js";
1
+ import { c as Extension } from "./types-BwODQ2T0.js";
2
2
  /** Bold text. `Mod-b`, `**text**` or `__text__`. */
3
3
  export declare const bold: () => Extension;
4
4
  /** Italic text. `Mod-i`, `*text*` or `_text_`. */
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * @packageDocumentation
6
6
  * @module @writeon/core
7
7
  */
8
- import { a as EditorLabel, c as Extension, d as MarkJSON, i as EditorEnableSource, m as TransactionScope, n as DocJSON, o as EditorOptions, p as NodeJSON, r as Editor, s as EditorSnapshot, t as Command } from "./types-Dy7Bm7XX.js";
8
+ import { a as EditorLabel, c as Extension, d as MarkJSON, i as EditorEnableSource, m as TransactionScope, n as DocJSON, o as EditorOptions, p as NodeJSON, r as Editor, s as EditorSnapshot, t as Command } from "./types-BwODQ2T0.js";
9
9
  /**
10
10
  * A debounced function with explicit control over pending calls.
11
11
  */
package/dist/index.js CHANGED
@@ -259,6 +259,9 @@ function createEditor(options) {
259
259
  get isEmpty() {
260
260
  return isDocEmpty(state.doc);
261
261
  },
262
+ get isDestroyed() {
263
+ return destroyed;
264
+ },
262
265
  focus(position) {
263
266
  assertAlive();
264
267
  if (!view) return;
package/dist/presets.d.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  * @module @writeon/core/presets
7
7
  */
8
8
  import { HeadingOptions, HistoryOptions, LinkOptions } from "./extensions.js";
9
- import { c as Extension } from "./types-Dy7Bm7XX.js";
9
+ import { c as Extension } from "./types-BwODQ2T0.js";
10
10
  /** Per-extension settings for `basic()`. `false` leaves an extension out. */
11
11
  export interface BasicOptions {
12
12
  /** Headings, or their options. */
@@ -125,6 +125,11 @@ interface Editor {
125
125
  }): void;
126
126
  /** Whether the document is a single empty text block. */
127
127
  readonly isEmpty: boolean;
128
+ /**
129
+ * Whether `destroy()` was called. UI that defers work (a timer, a
130
+ * microtask) checks it before reading the editor again.
131
+ */
132
+ readonly isDestroyed: boolean;
128
133
  /** Focus the editable area, optionally moving the cursor to the start or end. Does nothing when not mounted. */
129
134
  focus(position?: "start" | "end"): void;
130
135
  /** Remove focus from the editable area. */
package/llms-full.txt CHANGED
@@ -174,6 +174,7 @@ Or use the [default kit](/guide/kit.md): a themeable toolbar and content styles
174
174
 
175
175
  ## Next
176
176
 
177
+ - [Other frameworks](/guide/frameworks.md): Svelte, Angular, Solid, Lit and the rest.
177
178
  - [Core concepts](/guide/concepts.md): documents, extensions, commands and state.
178
179
  - [Saving and loading](/guide/saving.md): store JSON, convert to HTML or Markdown.
179
180
  - [Security](/guide/security.md): validate on the server before you store or render.
@@ -547,6 +548,239 @@ Things to know:
547
548
 
548
549
  ---
549
550
 
551
+ Source: /guide/frameworks.md
552
+
553
+ # Other frameworks
554
+
555
+ React and Vue have adapters (`@writeon/react`, `@writeon/vue`). Everything else works too, without one: the core is plain TypeScript with no framework inside, and the [default kit](/guide/kit.md) toolbar is plain DOM. Connecting the editor to a framework takes four steps:
556
+
557
+ | Step | What to call | When |
558
+ | --- | --- | --- |
559
+ | Create | `createEditor({ extensions, label })` | Once per component. No DOM needed yet. |
560
+ | Mount | `editor.mount(element)` | When the element exists, after the first render. |
561
+ | Read state | `editor.subscribe(listener)` and `editor.getSnapshot()` | Whenever the UI shows editor state (active marks, undo). |
562
+ | Destroy | `editor.destroy()` | When the component goes away. |
563
+
564
+ `subscribe()` calls the listener **without a value**, at most once per microtask, however many changes happened. Read the new state with `getSnapshot()`: it returns the same object until the state changes, so frameworks can compare by reference.
565
+
566
+ The examples below are built and tested in CI ([examples/svelte](https://github.com/writeon-editor/writeon/tree/main/examples/svelte), [examples/angular](https://github.com/writeon-editor/writeon/tree/main/examples/angular)).
567
+
568
+ ## Svelte
569
+
570
+ Svelte 5. `createSubscriber` turns `subscribe()` into a reactive value; two actions mount the editor and the kit toolbar.
571
+
572
+ **writeon.svelte.ts**
573
+
574
+ ```ts
575
+ import type { Editor, EditorSnapshot } from '@writeon/core'
576
+ import { mountToolbar } from '@writeon/kit'
577
+ import type { Action } from 'svelte/action'
578
+ import { createSubscriber } from 'svelte/reactivity'
579
+
580
+ // The editor state as a reactive value: read .current in markup or
581
+ // in $derived. It listens only while something reads it.
582
+ export function editorState(editor: Editor): {
583
+ readonly current: EditorSnapshot
584
+ } {
585
+ const subscribe = createSubscriber((update) =>
586
+ editor.subscribe(update),
587
+ )
588
+ return {
589
+ get current() {
590
+ subscribe()
591
+ return editor.getSnapshot()
592
+ },
593
+ }
594
+ }
595
+
596
+ // use:editorContent={editor} mounts the editable area. The editor is
597
+ // destroyed together with the element.
598
+ export const editorContent: Action<HTMLElement, Editor> = (
599
+ node,
600
+ editor,
601
+ ) => {
602
+ editor.mount(node)
603
+ return { destroy: () => editor.destroy() }
604
+ }
605
+
606
+ // use:kitToolbar={editor} adds the default kit toolbar.
607
+ export const kitToolbar: Action<HTMLElement, Editor> = (
608
+ node,
609
+ editor,
610
+ ) => {
611
+ const toolbar = mountToolbar(node, editor)
612
+ return { destroy: () => toolbar.destroy() }
613
+ }
614
+ ```
615
+
616
+ **App.svelte**
617
+
618
+ ```svelte
619
+ <script lang="ts">
620
+ import { createEditor } from '@writeon/core'
621
+ import { basic } from '@writeon/core/presets'
622
+ import '@writeon/kit/style.css'
623
+ import {
624
+ editorContent,
625
+ editorState,
626
+ kitToolbar,
627
+ } from './writeon.svelte'
628
+
629
+ // One editor per component. It attaches to the DOM in editorContent.
630
+ const editor = createEditor({
631
+ extensions: basic(),
632
+ label: 'Svelte example',
633
+ placeholder: 'Start writing…',
634
+ })
635
+ const state = editorState(editor)
636
+ const bold = $derived(state.current.marks.has('bold'))
637
+ </script>
638
+
639
+ <main>
640
+ <h1>Writeon · Svelte</h1>
641
+ <div class="rte-root">
642
+ <div use:kitToolbar={editor}></div>
643
+ <div use:editorContent={editor} class="rte-content"></div>
644
+ </div>
645
+ <p data-testid="status">
646
+ Block: {state.current.block.type} ·
647
+ Bold: {bold ? 'on' : 'off'} ·
648
+ Undo: {state.current.canUndo ? 'yes' : 'no'}
649
+ </p>
650
+ </main>
651
+
652
+ <style>
653
+ main {
654
+ max-width: 720px;
655
+ margin: 40px auto;
656
+ padding: 0 16px;
657
+ font-family: system-ui, sans-serif;
658
+ }
659
+ </style>
660
+ ```
661
+
662
+ With Svelte stores instead of runes: `readable(editor.getSnapshot(), (set) => editor.subscribe(() => set(editor.getSnapshot())))`. A store must pass its value to its subscribers, which is why `editor` itself is not one.
663
+
664
+ ## Angular
665
+
666
+ Angular 22, standalone and zoneless. A signal holds the snapshot; `afterNextRender` mounts the editor once the template is in the DOM.
667
+
668
+ **writeon.ts**
669
+
670
+ ```ts
671
+ import { DestroyRef, inject, type Signal, signal } from '@angular/core'
672
+ import type { Editor, EditorSnapshot } from '@writeon/core'
673
+
674
+ // The editor state as a signal. Call it in an injection context (a
675
+ // field initializer or the constructor); it stops listening when the
676
+ // component is destroyed.
677
+ export function editorState(editor: Editor): Signal<EditorSnapshot> {
678
+ const state = signal(editor.getSnapshot())
679
+ const off = editor.subscribe(() => state.set(editor.getSnapshot()))
680
+ inject(DestroyRef).onDestroy(off)
681
+ return state.asReadonly()
682
+ }
683
+ ```
684
+
685
+ **app.component.ts**
686
+
687
+ ```ts
688
+ import {
689
+ afterNextRender,
690
+ Component,
691
+ computed,
692
+ DestroyRef,
693
+ type ElementRef,
694
+ inject,
695
+ viewChild,
696
+ } from '@angular/core'
697
+ import { createEditor } from '@writeon/core'
698
+ import { basic } from '@writeon/core/presets'
699
+ import { type KitToolbar, mountToolbar } from '@writeon/kit'
700
+ import { editorState } from './writeon'
701
+
702
+ @Component({
703
+ selector: 'app-root',
704
+ template: `
705
+ <main>
706
+ <h1>Writeon · Angular</h1>
707
+ <div class="rte-root">
708
+ <div #toolbar></div>
709
+ <div #content class="rte-content"></div>
710
+ </div>
711
+ <p data-testid="status">
712
+ Block: {{ state().block.type }} ·
713
+ Bold: {{ bold() ? 'on' : 'off' }} ·
714
+ Undo: {{ state().canUndo ? 'yes' : 'no' }}
715
+ </p>
716
+ </main>
717
+ `,
718
+ styles: `
719
+ main {
720
+ max-width: 720px;
721
+ margin: 40px auto;
722
+ padding: 0 16px;
723
+ font-family: system-ui, sans-serif;
724
+ }
725
+ `,
726
+ })
727
+ export class AppComponent {
728
+ private readonly toolbarHost =
729
+ viewChild.required<ElementRef<HTMLElement>>('toolbar')
730
+ private readonly contentHost =
731
+ viewChild.required<ElementRef<HTMLElement>>('content')
732
+
733
+ // One editor per component. It attaches to the DOM after the first
734
+ // render, and the toolbar goes before the editor on destroy.
735
+ readonly editor = createEditor({
736
+ extensions: basic(),
737
+ label: 'Angular example',
738
+ placeholder: 'Start writing…',
739
+ })
740
+ readonly state = editorState(this.editor)
741
+ readonly bold = computed(() => this.state().marks.has('bold'))
742
+
743
+ constructor() {
744
+ let toolbar: KitToolbar | undefined
745
+ afterNextRender(() => {
746
+ this.editor.mount(this.contentHost().nativeElement)
747
+ toolbar = mountToolbar(
748
+ this.toolbarHost().nativeElement,
749
+ this.editor,
750
+ )
751
+ })
752
+ inject(DestroyRef).onDestroy(() => {
753
+ toolbar?.destroy()
754
+ this.editor.destroy()
755
+ })
756
+ }
757
+ }
758
+ ```
759
+
760
+ Add `@writeon/kit/style.css` to `styles` in `angular.json`. In an app that still uses zone.js, call `mount()` inside `NgZone.runOutsideAngular()`: the editor's own DOM events then do not run change detection on every keystroke, and the signal still updates the view.
761
+
762
+ ## Solid, Lit and others
763
+
764
+ The same four steps, in that framework's lifecycle:
765
+
766
+ | Framework | Mount | State | Destroy |
767
+ | --- | --- | --- | --- |
768
+ | Solid | `onMount` | `createSignal`, set from `subscribe()` | `onCleanup` |
769
+ | Lit | `firstUpdated` | a reactive property, set from `subscribe()` | `disconnectedCallback` |
770
+ | Plain JavaScript | when the element exists | `subscribe()` | when you remove it |
771
+
772
+ See [Getting started](/guide/getting-started.md#your-first-editor) for the plain JavaScript version.
773
+
774
+ ## Your own UI
775
+
776
+ The kit toolbar works everywhere. To build your own, the [headless UI](/guide/headless-ui.md) controllers do too: `applyProps(element, props)` puts their attributes and event handlers on your elements, as in its [plain JavaScript](/guide/headless-ui.md#plain-javascript) example. Call it from the controller's `subscribe()`.
777
+
778
+ ## Testing
779
+
780
+ The packages are ES modules only. Vitest runs them as they are. Jest needs its ESM mode: `node --experimental-vm-modules node_modules/jest/bin/jest.js`, with `testEnvironment: 'jsdom'` for tests that mount an editor.
781
+
782
+ ---
783
+
550
784
  Source: /guide/security.md
551
785
 
552
786
  # Security
@@ -3224,6 +3458,7 @@ An editor instance, created by `createEditor()`.
3224
3458
 
3225
3459
  | Property | Modifier | Type | Description |
3226
3460
  | ------ | ------ | ------ | ------ |
3461
+ | <a id="property-isdestroyed"></a> `isDestroyed` | `readonly` | `boolean` | Whether `destroy()` was called. UI that defers work (a timer, a microtask) checks it before reading the editor again. |
3227
3462
  | <a id="property-isempty"></a> `isEmpty` | `readonly` | `boolean` | Whether the document is a single empty text block. |
3228
3463
  | <a id="property-pm"></a> `pm` | `readonly` | \{ `schema`: `Schema`; `state`: `EditorState`; `view`: `EditorView` \| `null`; \} | Direct access to ProseMirror. **Not a stable API**: it may change in any minor release. |
3229
3464
  | `pm.schema` | `readonly` | `Schema` | The schema built from the extensions. |
package/llms.txt CHANGED
@@ -12,6 +12,7 @@ Every link below is plain Markdown. Everything in one file: [llms-full.txt](/llm
12
12
  - [Basic and advanced](/guide/basic-and-advanced.md): What the basic preset covers, and how to add task lists, code blocks, images, tables and mentions without loading them up front.
13
13
  - [Saving and loading](/guide/saving.md): Store the JSON document, save with debounce, and convert to and from HTML and Markdown.
14
14
  - [Server-side rendering](/guide/ssr.md): Server-side rendering with Next.js and Nuxt: what runs on the server and how the editor hydrates.
15
+ - [Other frameworks](/guide/frameworks.md): Use Writeon in Svelte, Angular or any other framework: four lifecycle steps, with tested examples.
15
16
  - [Security](/guide/security.md): Never trust editor output: validate documents on the server and render them as safe HTML.
16
17
  - [Accessibility](/guide/accessibility.md): What WCAG 2.2 AA means for the editor: keyboard use, ARIA patterns, labels and announcements.
17
18
  - [Default kit](/guide/kit.md): The ready-made UI: one CSS file, a toolbar and the advanced UI, with theming, dark mode and phone layout.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@writeon/core",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Framework-agnostic, headless, accessible rich text editor core built on ProseMirror",
5
5
  "license": "MIT",
6
6
  "author": "Ma'ruf Sheraliyev (https://github.com/mfs1011)",
@@ -25,27 +25,22 @@
25
25
  "sideEffects": false,
26
26
  "exports": {
27
27
  ".": {
28
- "@writeon/source": "./src/index.ts",
29
28
  "types": "./dist/index.d.ts",
30
29
  "import": "./dist/index.js"
31
30
  },
32
31
  "./extension": {
33
- "@writeon/source": "./src/entries/extension.ts",
34
32
  "types": "./dist/extension.d.ts",
35
33
  "import": "./dist/extension.js"
36
34
  },
37
35
  "./extensions": {
38
- "@writeon/source": "./src/extensions/index.ts",
39
36
  "types": "./dist/extensions.d.ts",
40
37
  "import": "./dist/extensions.js"
41
38
  },
42
39
  "./commands": {
43
- "@writeon/source": "./src/commands/index.ts",
44
40
  "types": "./dist/commands.d.ts",
45
41
  "import": "./dist/commands.js"
46
42
  },
47
43
  "./presets": {
48
- "@writeon/source": "./src/presets/index.ts",
49
44
  "types": "./dist/presets.d.ts",
50
45
  "import": "./dist/presets.js"
51
46
  },