solid-tiptap 0.9.0 → 1.0.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.
package/README.md CHANGED
@@ -24,10 +24,11 @@ You also need the Tiptap extensions you plan to use, such as `@tiptap/starter-ki
24
24
 
25
25
  ## Requirements
26
26
 
27
- - `solid-js` 1.7 or later.
27
+ - `solid-js` 2.0 or later.
28
28
  - `@tiptap/core` and `@tiptap/pm` 3.x.
29
+ - Node.js 22.12 or later, if you import the package in Node.js.
29
30
 
30
- Use `solid-tiptap@0.7` for Tiptap 2.
31
+ Solid 1.x users: use the `solid-1` branch, or the previous major (`solid-tiptap@0.9` for Solid 1.x, `solid-tiptap@0.7` for Solid 1.x with Tiptap 2).
31
32
 
32
33
  ## Usage
33
34
 
@@ -50,7 +51,7 @@ function App() {
50
51
 
51
52
  `createTiptapEditor` returns an accessor for the editor.
52
53
 
53
- - The editor is created in an effect, after the element is mounted. The accessor returns `undefined` until then.
54
+ - The editor is created after the component mounts, once `element` is set. The accessor returns `undefined` until then.
54
55
  - The options function is tracked. When a signal it reads changes, the old editor is destroyed and a new one is created.
55
56
  - The editor is destroyed when the owning component unmounts.
56
57
 
@@ -88,6 +89,21 @@ const editor = createTiptapEditor(() => ({ ... }));
88
89
 
89
90
  `keyed` passes the editor itself to the children. Without it, Solid passes an accessor, so you would write `editor={instance()}`.
90
91
 
92
+ ### Signal refs
93
+
94
+ `element` also accepts `undefined` and `null`. The editor waits until the element is set, so a signal ref works without a non-null assertion.
95
+
96
+ ```tsx
97
+ const [element, setElement] = createSignal<HTMLDivElement>();
98
+
99
+ const editor = createTiptapEditor(() => ({
100
+ element: element(),
101
+ extensions: [StarterKit],
102
+ }));
103
+
104
+ return <div ref={setElement} />;
105
+ ```
106
+
91
107
  ### Bubble and floating menus
92
108
 
93
109
  Tiptap's `BubbleMenu` and `FloatingMenu` extensions take an `element` option. Pass a mounted element the same way you pass the editor element.
@@ -129,13 +145,21 @@ const isBold = createEditorTransaction(
129
145
  (editor) => editor.isActive('bold'),
130
146
  );
131
147
 
132
- createEffect(() => {
133
- if (isBold()) {
148
+ createEffect(isBold, (bold) => {
149
+ if (bold) {
134
150
  // ...
135
151
  }
136
152
  });
137
153
  ```
138
154
 
155
+ Like any Solid 2 signal, the new value is visible after Solid applies queued updates. Call `flush()` from `solid-js` if you need it right away, for example in a test.
156
+
157
+ ```ts
158
+ editor.commands.setContent('<p>Bye</p>');
159
+ flush();
160
+ html(); // '<p>Bye</p>'
161
+ ```
162
+
139
163
  The read function receives whatever the first accessor returns. If that can be `undefined`, handle it in the read function.
140
164
 
141
165
  ```ts
@@ -161,16 +185,15 @@ Each helper wraps `createEditorTransaction` for one part of the [Editor API](htt
161
185
  ```ts
162
186
  const html = useEditorHTML(editor);
163
187
 
164
- createEffect(() => {
165
- saveHTML(html());
188
+ createEffect(html, (value) => {
189
+ saveHTML(value);
166
190
  });
167
191
  ```
168
192
 
169
193
  ```ts
170
194
  const json = useEditorJSON(editor);
171
195
 
172
- createEffect(() => {
173
- const data = json();
196
+ createEffect(json, (data) => {
174
197
  if (data) {
175
198
  uploadJSON(data);
176
199
  }
@@ -196,7 +219,7 @@ const isCentered = useEditorIsActive(editor, { textAlign: 'center' });
196
219
 
197
220
  Creates a Tiptap editor. Also exported as `createEditor`, `useEditor` and `useTiptapEditor`.
198
221
 
199
- - `options` is a function that returns Tiptap's [editor options](https://tiptap.dev/docs/editor/api/editor#settings). `element` is required.
222
+ - `options` is a function that returns Tiptap's [editor options](https://tiptap.dev/docs/editor/api/editor#settings). No editor is created while `element` is `undefined` or `null`.
200
223
  - Returns `() => Editor | undefined`.
201
224
 
202
225
  ### `createEditorTransaction(editor, read)`
@@ -207,7 +230,7 @@ Creates a Tiptap editor. Also exported as `createEditor`, `useEditor` and `useTi
207
230
 
208
231
  ### Types
209
232
 
210
- - `UseEditorOptions<T extends HTMLElement>` is the options type for `createTiptapEditor`.
233
+ - `UseEditorOptions` is the options type for `createTiptapEditor`. Its `element` is `HTMLElement | null | undefined`.
211
234
  - `BaseEditorOptions` is Tiptap's `EditorOptions` without `element`, with every field optional.
212
235
 
213
236
  ## Sponsors
package/dist/index.d.ts CHANGED
@@ -2,8 +2,8 @@ import { Editor, EditorOptions, JSONContent } from "@tiptap/core";
2
2
  //#region src/Editor.d.ts
3
3
  type EditorRef = Editor | ((editor: Editor) => void);
4
4
  type BaseEditorOptions = Omit<Partial<EditorOptions>, 'element'>;
5
- interface UseEditorOptions<T extends HTMLElement> extends BaseEditorOptions {
6
- element: T;
5
+ interface UseEditorOptions extends BaseEditorOptions {
6
+ element: HTMLElement | null | undefined;
7
7
  }
8
8
  /**
9
9
  * Reads a value from the editor and reads it again after every transaction.
@@ -12,10 +12,11 @@ interface UseEditorOptions<T extends HTMLElement> extends BaseEditorOptions {
12
12
  export declare function createEditorTransaction<T, V extends Editor | undefined>(instance: () => V, read: (value: V) => T): () => T;
13
13
  /**
14
14
  * Creates a Tiptap editor from the given options.
15
- * The editor is destroyed and created again when a signal read by `props` changes.
15
+ * The editor is created once `element` is set.
16
+ * It is destroyed and created again when a signal read by `props` changes.
16
17
  * It is also destroyed when the owner is disposed.
17
18
  */
18
- declare function useEditor<T extends HTMLElement>(props: () => UseEditorOptions<T>): () => Editor | undefined;
19
+ declare function useEditor(props: () => UseEditorOptions): () => Editor | undefined;
19
20
  /** Returns the editor content as HTML. */
20
21
  export declare function useEditorHTML(editor: () => Editor | undefined): () => string | undefined;
21
22
  /** Returns the editor content as Tiptap JSON. */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/Editor.ts"],"mappings":";;KAIY,YAAY,WAAW,QAAQ;KAE/B,oBAAoB,KAAK,QAAQ;UAE5B,iBAAiB,UAAU,qBAAqB;EAC/D,SAAS;;;;;;wBAOK,wBAAwB,GAAG,UAAU,oBACnD,gBAAgB,GAChB,OAAO,OAAO,MAAM,UACb;;;;;;iBA4Be,UAAU,UAAU,aAC1C,aAAa,iBAAiB,WACvB;;wBAmBO,cAAc,cAAc;;wBAK5B,cAAc,cAAc,2BAA2B;;;;;wBAQvD,kBACd,cAAc,uBACX,OACE,oBAAoB,UAAU,4BAC9B,SAAS;;wBAeA,iBAAiB,cAAc;;wBAK/B,oBAAoB,cAAc;;wBAKlC,mBAAmB,cAAc"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/Editor.ts"],"mappings":";;KAIY,YAAY,WAAW,QAAQ;KAE/B,oBAAoB,KAAK,QAAQ;UAE5B,yBAAyB;EAExC,SAAS;;;;;;wBAOK,wBAAwB,GAAG,UAAU,oBACnD,gBAAgB,GAChB,OAAO,OAAO,MAAM,UACb;;;;;;;iBA8Be,UAAU,aAAa,yBAAyB;;wBA6BxD,cAAc,cAAc;;wBAK5B,cAAc,cAAc,2BAA2B;;;;;wBAQvD,kBACd,cAAc,uBACX,OACE,oBAAoB,UAAU,4BAC9B,SAAS;;wBAeA,iBAAiB,cAAc;;wBAK/B,oBAAoB,cAAc;;wBAKlC,mBAAmB,cAAc"}
package/dist/index.js CHANGED
@@ -1,23 +1,24 @@
1
1
  import { Editor } from "@tiptap/core";
2
- import { createEffect, createSignal, onCleanup } from "solid-js";
2
+ import { createEffect, createSignal, onSettled } from "solid-js";
3
3
  //#region src/Editor.ts
4
4
  /**
5
5
  * Reads a value from the editor and reads it again after every transaction.
6
6
  * `read` also receives `undefined` while `instance` returns no editor.
7
7
  */
8
8
  function createEditorTransaction(instance, read) {
9
- const [depend, update] = createSignal(void 0, { equals: false });
10
- function forceUpdate() {
11
- update();
12
- }
13
- createEffect(() => {
14
- const editor = instance();
15
- if (editor) {
16
- editor.on("transaction", forceUpdate);
17
- onCleanup(() => {
18
- editor.off("transaction", forceUpdate);
19
- });
20
- }
9
+ const [depend, update] = createSignal(void 0, {
10
+ equals: false,
11
+ ownedWrite: true
12
+ });
13
+ createEffect(instance, (editor) => {
14
+ if (!editor) return;
15
+ const forceUpdate = () => {
16
+ update();
17
+ };
18
+ editor.on("transaction", forceUpdate);
19
+ return () => {
20
+ editor.off("transaction", forceUpdate);
21
+ };
21
22
  });
22
23
  return () => {
23
24
  depend();
@@ -26,27 +27,33 @@ function createEditorTransaction(instance, read) {
26
27
  }
27
28
  /**
28
29
  * Creates a Tiptap editor from the given options.
29
- * The editor is destroyed and created again when a signal read by `props` changes.
30
+ * The editor is created once `element` is set.
31
+ * It is destroyed and created again when a signal read by `props` changes.
30
32
  * It is also destroyed when the owner is disposed.
31
33
  */
32
34
  function useEditor(props) {
33
- const [signal, setSignal] = createSignal();
34
- createEffect(() => {
35
- const instance = new Editor({ ...props() });
36
- onCleanup(() => {
37
- instance.destroy();
38
- });
35
+ const [signal, setSignal] = createSignal(void 0, { ownedWrite: true });
36
+ const [settled, setSettled] = createSignal(false, { ownedWrite: true });
37
+ onSettled(() => {
38
+ setSettled(true);
39
+ });
40
+ createEffect(() => settled() ? props() : void 0, (options) => {
41
+ if (!options?.element) return;
42
+ const instance = new Editor({ ...options });
39
43
  setSignal(instance);
44
+ return () => {
45
+ instance.destroy();
46
+ };
40
47
  });
41
48
  return signal;
42
49
  }
43
50
  /** Returns the editor content as HTML. */
44
51
  function useEditorHTML(editor) {
45
- return createEditorTransaction(editor, (instance) => instance === null || instance === void 0 ? void 0 : instance.getHTML());
52
+ return createEditorTransaction(editor, (instance) => instance?.getHTML());
46
53
  }
47
54
  /** Returns the editor content as Tiptap JSON. */
48
55
  function useEditorJSON(editor) {
49
- return createEditorTransaction(editor, (instance) => instance === null || instance === void 0 ? void 0 : instance.getJSON());
56
+ return createEditorTransaction(editor, (instance) => instance?.getJSON());
50
57
  }
51
58
  /**
52
59
  * Returns whether the node or mark at the selection is active.
@@ -54,23 +61,23 @@ function useEditorJSON(editor) {
54
61
  */
55
62
  function useEditorIsActive(editor, ...args) {
56
63
  return createEditorTransaction(editor, (instance) => {
57
- if (args.length === 2) return instance === null || instance === void 0 ? void 0 : instance.isActive(args[0](), args[1]);
64
+ if (args.length === 2) return instance?.isActive(args[0](), args[1]);
58
65
  const [first] = args;
59
- if (typeof first === "function") return instance === null || instance === void 0 ? void 0 : instance.isActive(first());
60
- return instance === null || instance === void 0 ? void 0 : instance.isActive(first);
66
+ if (typeof first === "function") return instance?.isActive(first());
67
+ return instance?.isActive(first);
61
68
  });
62
69
  }
63
70
  /** Returns whether the editor has no content. */
64
71
  function useEditorIsEmpty(editor) {
65
- return createEditorTransaction(editor, (instance) => instance === null || instance === void 0 ? void 0 : instance.isEmpty);
72
+ return createEditorTransaction(editor, (instance) => instance?.isEmpty);
66
73
  }
67
74
  /** Returns whether the editor accepts input. */
68
75
  function useEditorIsEditable(editor) {
69
- return createEditorTransaction(editor, (instance) => instance === null || instance === void 0 ? void 0 : instance.isEditable);
76
+ return createEditorTransaction(editor, (instance) => instance?.isEditable);
70
77
  }
71
78
  /** Returns whether the editor has focus. */
72
79
  function useEditorIsFocused(editor) {
73
- return createEditorTransaction(editor, (instance) => instance === null || instance === void 0 ? void 0 : instance.isFocused);
80
+ return createEditorTransaction(editor, (instance) => instance?.isFocused);
74
81
  }
75
82
  //#endregion
76
83
  export { useEditor as createEditor, useEditor as createTiptapEditor, useEditor, useEditor as useTiptapEditor, createEditorTransaction, useEditorHTML, useEditorIsActive, useEditorIsEditable, useEditorIsEmpty, useEditorIsFocused, useEditorJSON };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/Editor.ts"],"sourcesContent":["import type { EditorOptions, JSONContent } from '@tiptap/core';\nimport { Editor } from '@tiptap/core';\nimport { createEffect, createSignal, onCleanup } from 'solid-js';\n\nexport type EditorRef = Editor | ((editor: Editor) => void);\n\nexport type BaseEditorOptions = Omit<Partial<EditorOptions>, 'element'>;\n\nexport interface UseEditorOptions<T extends HTMLElement> extends BaseEditorOptions {\n element: T;\n}\n\n/**\n * Reads a value from the editor and reads it again after every transaction.\n * `read` also receives `undefined` while `instance` returns no editor.\n */\nexport function createEditorTransaction<T, V extends Editor | undefined>(\n instance: () => V,\n read: (value: V) => T,\n): () => T {\n const [depend, update] = createSignal(undefined, { equals: false });\n\n function forceUpdate(): void {\n update();\n }\n\n createEffect(() => {\n const editor = instance();\n if (editor) {\n editor.on('transaction', forceUpdate);\n onCleanup(() => {\n editor.off('transaction', forceUpdate);\n });\n }\n });\n\n return () => {\n depend();\n return read(instance());\n };\n}\n\n/**\n * Creates a Tiptap editor from the given options.\n * The editor is destroyed and created again when a signal read by `props` changes.\n * It is also destroyed when the owner is disposed.\n */\nexport default function useEditor<T extends HTMLElement>(\n props: () => UseEditorOptions<T>,\n): () => Editor | undefined {\n const [signal, setSignal] = createSignal<Editor>();\n\n createEffect(() => {\n const instance = new Editor({\n ...props(),\n });\n\n onCleanup(() => {\n instance.destroy();\n });\n\n setSignal(instance);\n });\n\n return signal;\n}\n\n/** Returns the editor content as HTML. */\nexport function useEditorHTML(editor: () => Editor | undefined): () => string | undefined {\n return createEditorTransaction(editor, (instance) => instance?.getHTML());\n}\n\n/** Returns the editor content as Tiptap JSON. */\nexport function useEditorJSON(editor: () => Editor | undefined): () => JSONContent | undefined {\n return createEditorTransaction(editor, (instance) => instance?.getJSON());\n}\n\n/**\n * Returns whether the node or mark at the selection is active.\n * Pass a node or mark name with optional attributes, or only the attributes.\n */\nexport function useEditorIsActive(\n editor: () => Editor | undefined,\n ...args:\n | [name: () => string, options?: Record<string, unknown>]\n | [options: Record<string, unknown>]\n): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => {\n if (args.length === 2) {\n return instance?.isActive(args[0](), args[1]);\n }\n const [first] = args;\n if (typeof first === 'function') {\n return instance?.isActive(first());\n }\n return instance?.isActive(first);\n });\n}\n\n/** Returns whether the editor has no content. */\nexport function useEditorIsEmpty(editor: () => Editor | undefined): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => instance?.isEmpty);\n}\n\n/** Returns whether the editor accepts input. */\nexport function useEditorIsEditable(editor: () => Editor | undefined): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => instance?.isEditable);\n}\n\n/** Returns whether the editor has focus. */\nexport function useEditorIsFocused(editor: () => Editor | undefined): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => instance?.isFocused);\n}\n"],"mappings":";;;;;;;AAgBA,SAAgB,wBACd,UACA,MACS;CACT,MAAM,CAAC,QAAQ,UAAU,aAAa,KAAA,GAAW,EAAE,QAAQ,MAAM,CAAC;CAElE,SAAS,cAAoB;EAC3B,OAAO;CACT;CAEA,mBAAmB;EACjB,MAAM,SAAS,SAAS;EACxB,IAAI,QAAQ;GACV,OAAO,GAAG,eAAe,WAAW;GACpC,gBAAgB;IACd,OAAO,IAAI,eAAe,WAAW;GACvC,CAAC;EACH;CACF,CAAC;CAED,aAAa;EACX,OAAO;EACP,OAAO,KAAK,SAAS,CAAC;CACxB;AACF;;;;;;AAOA,SAAwB,UACtB,OAC0B;CAC1B,MAAM,CAAC,QAAQ,aAAa,aAAqB;CAEjD,mBAAmB;EACjB,MAAM,WAAW,IAAI,OAAO,EAC1B,GAAG,MAAM,EACX,CAAC;EAED,gBAAgB;GACd,SAAS,QAAQ;EACnB,CAAC;EAED,UAAU,QAAQ;CACpB,CAAC;CAED,OAAO;AACT;;AAGA,SAAgB,cAAc,QAA4D;CACxF,OAAO,wBAAwB,SAAS,aAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAa,SAAU,QAAQ,CAAC;AAC1E;;AAGA,SAAgB,cAAc,QAAiE;CAC7F,OAAO,wBAAwB,SAAS,aAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAa,SAAU,QAAQ,CAAC;AAC1E;;;;;AAMA,SAAgB,kBACd,QACA,GAAG,MAGwB;CAC3B,OAAO,wBAAwB,SAAS,aAAa;EACnD,IAAI,KAAK,WAAW,GAClB,OAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAO,SAAU,SAAS,KAAK,EAAE,CAAC,GAAG,KAAK,EAAE;EAE9C,MAAM,CAAC,SAAS;EAChB,IAAI,OAAO,UAAU,YACnB,OAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAO,SAAU,SAAS,MAAM,CAAC;EAEnC,OAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAO,SAAU,SAAS,KAAK;CACjC,CAAC;AACH;;AAGA,SAAgB,iBAAiB,QAA6D;CAC5F,OAAO,wBAAwB,SAAS,aAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAa,SAAU,OAAO;AACxE;;AAGA,SAAgB,oBAAoB,QAA6D;CAC/F,OAAO,wBAAwB,SAAS,aAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAa,SAAU,UAAU;AAC3E;;AAGA,SAAgB,mBAAmB,QAA6D;CAC9F,OAAO,wBAAwB,SAAS,aAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAa,SAAU,SAAS;AAC1E"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/Editor.ts"],"sourcesContent":["import type { EditorOptions, JSONContent } from '@tiptap/core';\nimport { Editor } from '@tiptap/core';\nimport { createEffect, createSignal, onSettled } from 'solid-js';\n\nexport type EditorRef = Editor | ((editor: Editor) => void);\n\nexport type BaseEditorOptions = Omit<Partial<EditorOptions>, 'element'>;\n\nexport interface UseEditorOptions extends BaseEditorOptions {\n // The editor is created once this is set.\n element: HTMLElement | null | undefined;\n}\n\n/**\n * Reads a value from the editor and reads it again after every transaction.\n * `read` also receives `undefined` while `instance` returns no editor.\n */\nexport function createEditorTransaction<T, V extends Editor | undefined>(\n instance: () => V,\n read: (value: V) => T,\n): () => T {\n // Tiptap emits transactions from its own listeners, which can run inside a\n // reactive scope, so the signal allows writes from owned scopes.\n const [depend, update] = createSignal(undefined, { equals: false, ownedWrite: true });\n\n createEffect(instance, (editor) => {\n if (!editor) {\n return undefined;\n }\n const forceUpdate = (): void => {\n update();\n };\n editor.on('transaction', forceUpdate);\n return () => {\n editor.off('transaction', forceUpdate);\n };\n });\n\n return () => {\n depend();\n return read(instance());\n };\n}\n\n/**\n * Creates a Tiptap editor from the given options.\n * The editor is created once `element` is set.\n * It is destroyed and created again when a signal read by `props` changes.\n * It is also destroyed when the owner is disposed.\n */\nexport default function useEditor(props: () => UseEditorOptions): () => Editor | undefined {\n // The editor is written from the effect, which runs in an owned scope.\n const [signal, setSignal] = createSignal<Editor | undefined>(undefined, { ownedWrite: true });\n\n // Refs are assigned after the component body runs. Reading the options only\n // after the first settle lets `element` come from a plain `let` ref too.\n const [settled, setSettled] = createSignal(false, { ownedWrite: true });\n onSettled(() => {\n setSettled(true);\n });\n\n createEffect(\n () => (settled() ? props() : undefined),\n (options) => {\n if (!options?.element) {\n return undefined;\n }\n const instance = new Editor({ ...options });\n setSignal(instance);\n return () => {\n instance.destroy();\n };\n },\n );\n\n return signal;\n}\n\n/** Returns the editor content as HTML. */\nexport function useEditorHTML(editor: () => Editor | undefined): () => string | undefined {\n return createEditorTransaction(editor, (instance) => instance?.getHTML());\n}\n\n/** Returns the editor content as Tiptap JSON. */\nexport function useEditorJSON(editor: () => Editor | undefined): () => JSONContent | undefined {\n return createEditorTransaction(editor, (instance) => instance?.getJSON());\n}\n\n/**\n * Returns whether the node or mark at the selection is active.\n * Pass a node or mark name with optional attributes, or only the attributes.\n */\nexport function useEditorIsActive(\n editor: () => Editor | undefined,\n ...args:\n | [name: () => string, options?: Record<string, unknown>]\n | [options: Record<string, unknown>]\n): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => {\n if (args.length === 2) {\n return instance?.isActive(args[0](), args[1]);\n }\n const [first] = args;\n if (typeof first === 'function') {\n return instance?.isActive(first());\n }\n return instance?.isActive(first);\n });\n}\n\n/** Returns whether the editor has no content. */\nexport function useEditorIsEmpty(editor: () => Editor | undefined): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => instance?.isEmpty);\n}\n\n/** Returns whether the editor accepts input. */\nexport function useEditorIsEditable(editor: () => Editor | undefined): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => instance?.isEditable);\n}\n\n/** Returns whether the editor has focus. */\nexport function useEditorIsFocused(editor: () => Editor | undefined): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => instance?.isFocused);\n}\n"],"mappings":";;;;;;;AAiBA,SAAgB,wBACd,UACA,MACS;CAGT,MAAM,CAAC,QAAQ,UAAU,aAAa,KAAA,GAAW;EAAE,QAAQ;EAAO,YAAY;CAAK,CAAC;CAEpF,aAAa,WAAW,WAAW;EACjC,IAAI,CAAC,QACH;EAEF,MAAM,oBAA0B;GAC9B,OAAO;EACT;EACA,OAAO,GAAG,eAAe,WAAW;EACpC,aAAa;GACX,OAAO,IAAI,eAAe,WAAW;EACvC;CACF,CAAC;CAED,aAAa;EACX,OAAO;EACP,OAAO,KAAK,SAAS,CAAC;CACxB;AACF;;;;;;;AAQA,SAAwB,UAAU,OAAyD;CAEzF,MAAM,CAAC,QAAQ,aAAa,aAAiC,KAAA,GAAW,EAAE,YAAY,KAAK,CAAC;CAI5F,MAAM,CAAC,SAAS,cAAc,aAAa,OAAO,EAAE,YAAY,KAAK,CAAC;CACtE,gBAAgB;EACd,WAAW,IAAI;CACjB,CAAC;CAED,mBACS,QAAQ,IAAI,MAAM,IAAI,KAAA,IAC5B,YAAY;EACX,IAAI,CAAC,SAAS,SACZ;EAEF,MAAM,WAAW,IAAI,OAAO,EAAE,GAAG,QAAQ,CAAC;EAC1C,UAAU,QAAQ;EAClB,aAAa;GACX,SAAS,QAAQ;EACnB;CACF,CACF;CAEA,OAAO;AACT;;AAGA,SAAgB,cAAc,QAA4D;CACxF,OAAO,wBAAwB,SAAS,aAAa,UAAU,QAAQ,CAAC;AAC1E;;AAGA,SAAgB,cAAc,QAAiE;CAC7F,OAAO,wBAAwB,SAAS,aAAa,UAAU,QAAQ,CAAC;AAC1E;;;;;AAMA,SAAgB,kBACd,QACA,GAAG,MAGwB;CAC3B,OAAO,wBAAwB,SAAS,aAAa;EACnD,IAAI,KAAK,WAAW,GAClB,OAAO,UAAU,SAAS,KAAK,EAAE,CAAC,GAAG,KAAK,EAAE;EAE9C,MAAM,CAAC,SAAS;EAChB,IAAI,OAAO,UAAU,YACnB,OAAO,UAAU,SAAS,MAAM,CAAC;EAEnC,OAAO,UAAU,SAAS,KAAK;CACjC,CAAC;AACH;;AAGA,SAAgB,iBAAiB,QAA6D;CAC5F,OAAO,wBAAwB,SAAS,aAAa,UAAU,OAAO;AACxE;;AAGA,SAAgB,oBAAoB,QAA6D;CAC/F,OAAO,wBAAwB,SAAS,aAAa,UAAU,UAAU;AAC3E;;AAGA,SAAgB,mBAAmB,QAA6D;CAC9F,OAAO,wBAAwB,SAAS,aAAa,UAAU,SAAS;AAC1E"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-tiptap",
3
- "version": "0.9.0",
3
+ "version": "1.0.0",
4
4
  "description": "SolidJS bindings for Tiptap",
5
5
  "keywords": [
6
6
  "editor",
@@ -26,37 +26,39 @@
26
26
  ],
27
27
  "type": "module",
28
28
  "sideEffects": false,
29
- "main": "./dist/index.cjs",
30
- "module": "./dist/index.js",
31
- "types": "./dist/index.d.cts",
32
29
  "exports": {
33
- ".": {
34
- "import": "./dist/index.js",
35
- "require": "./dist/index.cjs"
36
- },
30
+ ".": "./dist/index.js",
37
31
  "./package.json": "./package.json"
38
32
  },
39
33
  "publishConfig": {
40
34
  "access": "public"
41
35
  },
42
36
  "devDependencies": {
37
+ "@solidjs/vite-plugin": "3.0.0",
38
+ "@solidjs/web": "2.0.0",
43
39
  "@tiptap/core": "^3.31.4",
44
40
  "@tiptap/pm": "^3.31.4",
41
+ "@tiptap/starter-kit": "^3.31.4",
42
+ "@types/node": "^26.6.4",
43
+ "jsdom": "^30.1.1",
45
44
  "publint": "^0.3.25",
46
- "solid-js": "^1.9.15",
45
+ "solid-js": "2.0.0",
47
46
  "tsdown": "^0.23.0",
48
- "typescript": "^7.0.2"
47
+ "typescript": "^7.0.2",
48
+ "vitest": "^5.0.3"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "@tiptap/core": "^3",
52
52
  "@tiptap/pm": "^3",
53
- "solid-js": "^1.7"
53
+ "solid-js": "^2.0.0"
54
54
  },
55
55
  "engines": {
56
- "node": ">=18"
56
+ "node": ">=22.12.0"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "tsdown",
60
+ "test": "vitest run",
61
+ "test:watch": "vitest",
60
62
  "type-check": "tsc --noEmit",
61
63
  "watch": "tsdown --watch"
62
64
  }
package/src/Editor.ts CHANGED
@@ -1,13 +1,14 @@
1
1
  import type { EditorOptions, JSONContent } from '@tiptap/core';
2
2
  import { Editor } from '@tiptap/core';
3
- import { createEffect, createSignal, onCleanup } from 'solid-js';
3
+ import { createEffect, createSignal, onSettled } from 'solid-js';
4
4
 
5
5
  export type EditorRef = Editor | ((editor: Editor) => void);
6
6
 
7
7
  export type BaseEditorOptions = Omit<Partial<EditorOptions>, 'element'>;
8
8
 
9
- export interface UseEditorOptions<T extends HTMLElement> extends BaseEditorOptions {
10
- element: T;
9
+ export interface UseEditorOptions extends BaseEditorOptions {
10
+ // The editor is created once this is set.
11
+ element: HTMLElement | null | undefined;
11
12
  }
12
13
 
13
14
  /**
@@ -18,20 +19,21 @@ export function createEditorTransaction<T, V extends Editor | undefined>(
18
19
  instance: () => V,
19
20
  read: (value: V) => T,
20
21
  ): () => T {
21
- const [depend, update] = createSignal(undefined, { equals: false });
22
-
23
- function forceUpdate(): void {
24
- update();
25
- }
26
-
27
- createEffect(() => {
28
- const editor = instance();
29
- if (editor) {
30
- editor.on('transaction', forceUpdate);
31
- onCleanup(() => {
32
- editor.off('transaction', forceUpdate);
33
- });
22
+ // Tiptap emits transactions from its own listeners, which can run inside a
23
+ // reactive scope, so the signal allows writes from owned scopes.
24
+ const [depend, update] = createSignal(undefined, { equals: false, ownedWrite: true });
25
+
26
+ createEffect(instance, (editor) => {
27
+ if (!editor) {
28
+ return undefined;
34
29
  }
30
+ const forceUpdate = (): void => {
31
+ update();
32
+ };
33
+ editor.on('transaction', forceUpdate);
34
+ return () => {
35
+ editor.off('transaction', forceUpdate);
36
+ };
35
37
  });
36
38
 
37
39
  return () => {
@@ -42,26 +44,35 @@ export function createEditorTransaction<T, V extends Editor | undefined>(
42
44
 
43
45
  /**
44
46
  * Creates a Tiptap editor from the given options.
45
- * The editor is destroyed and created again when a signal read by `props` changes.
47
+ * The editor is created once `element` is set.
48
+ * It is destroyed and created again when a signal read by `props` changes.
46
49
  * It is also destroyed when the owner is disposed.
47
50
  */
48
- export default function useEditor<T extends HTMLElement>(
49
- props: () => UseEditorOptions<T>,
50
- ): () => Editor | undefined {
51
- const [signal, setSignal] = createSignal<Editor>();
52
-
53
- createEffect(() => {
54
- const instance = new Editor({
55
- ...props(),
56
- });
57
-
58
- onCleanup(() => {
59
- instance.destroy();
60
- });
61
-
62
- setSignal(instance);
51
+ export default function useEditor(props: () => UseEditorOptions): () => Editor | undefined {
52
+ // The editor is written from the effect, which runs in an owned scope.
53
+ const [signal, setSignal] = createSignal<Editor | undefined>(undefined, { ownedWrite: true });
54
+
55
+ // Refs are assigned after the component body runs. Reading the options only
56
+ // after the first settle lets `element` come from a plain `let` ref too.
57
+ const [settled, setSettled] = createSignal(false, { ownedWrite: true });
58
+ onSettled(() => {
59
+ setSettled(true);
63
60
  });
64
61
 
62
+ createEffect(
63
+ () => (settled() ? props() : undefined),
64
+ (options) => {
65
+ if (!options?.element) {
66
+ return undefined;
67
+ }
68
+ const instance = new Editor({ ...options });
69
+ setSignal(instance);
70
+ return () => {
71
+ instance.destroy();
72
+ };
73
+ },
74
+ );
75
+
65
76
  return signal;
66
77
  }
67
78
 
package/dist/index.cjs DELETED
@@ -1,89 +0,0 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- let _tiptap_core = require("@tiptap/core");
3
- let solid_js = require("solid-js");
4
- //#region src/Editor.ts
5
- /**
6
- * Reads a value from the editor and reads it again after every transaction.
7
- * `read` also receives `undefined` while `instance` returns no editor.
8
- */
9
- function createEditorTransaction(instance, read) {
10
- const [depend, update] = (0, solid_js.createSignal)(void 0, { equals: false });
11
- function forceUpdate() {
12
- update();
13
- }
14
- (0, solid_js.createEffect)(() => {
15
- const editor = instance();
16
- if (editor) {
17
- editor.on("transaction", forceUpdate);
18
- (0, solid_js.onCleanup)(() => {
19
- editor.off("transaction", forceUpdate);
20
- });
21
- }
22
- });
23
- return () => {
24
- depend();
25
- return read(instance());
26
- };
27
- }
28
- /**
29
- * Creates a Tiptap editor from the given options.
30
- * The editor is destroyed and created again when a signal read by `props` changes.
31
- * It is also destroyed when the owner is disposed.
32
- */
33
- function useEditor(props) {
34
- const [signal, setSignal] = (0, solid_js.createSignal)();
35
- (0, solid_js.createEffect)(() => {
36
- const instance = new _tiptap_core.Editor({ ...props() });
37
- (0, solid_js.onCleanup)(() => {
38
- instance.destroy();
39
- });
40
- setSignal(instance);
41
- });
42
- return signal;
43
- }
44
- /** Returns the editor content as HTML. */
45
- function useEditorHTML(editor) {
46
- return createEditorTransaction(editor, (instance) => instance === null || instance === void 0 ? void 0 : instance.getHTML());
47
- }
48
- /** Returns the editor content as Tiptap JSON. */
49
- function useEditorJSON(editor) {
50
- return createEditorTransaction(editor, (instance) => instance === null || instance === void 0 ? void 0 : instance.getJSON());
51
- }
52
- /**
53
- * Returns whether the node or mark at the selection is active.
54
- * Pass a node or mark name with optional attributes, or only the attributes.
55
- */
56
- function useEditorIsActive(editor, ...args) {
57
- return createEditorTransaction(editor, (instance) => {
58
- if (args.length === 2) return instance === null || instance === void 0 ? void 0 : instance.isActive(args[0](), args[1]);
59
- const [first] = args;
60
- if (typeof first === "function") return instance === null || instance === void 0 ? void 0 : instance.isActive(first());
61
- return instance === null || instance === void 0 ? void 0 : instance.isActive(first);
62
- });
63
- }
64
- /** Returns whether the editor has no content. */
65
- function useEditorIsEmpty(editor) {
66
- return createEditorTransaction(editor, (instance) => instance === null || instance === void 0 ? void 0 : instance.isEmpty);
67
- }
68
- /** Returns whether the editor accepts input. */
69
- function useEditorIsEditable(editor) {
70
- return createEditorTransaction(editor, (instance) => instance === null || instance === void 0 ? void 0 : instance.isEditable);
71
- }
72
- /** Returns whether the editor has focus. */
73
- function useEditorIsFocused(editor) {
74
- return createEditorTransaction(editor, (instance) => instance === null || instance === void 0 ? void 0 : instance.isFocused);
75
- }
76
- //#endregion
77
- exports.createEditor = useEditor;
78
- exports.createEditorTransaction = createEditorTransaction;
79
- exports.createTiptapEditor = useEditor;
80
- exports.useEditor = useEditor;
81
- exports.useEditorHTML = useEditorHTML;
82
- exports.useEditorIsActive = useEditorIsActive;
83
- exports.useEditorIsEditable = useEditorIsEditable;
84
- exports.useEditorIsEmpty = useEditorIsEmpty;
85
- exports.useEditorIsFocused = useEditorIsFocused;
86
- exports.useEditorJSON = useEditorJSON;
87
- exports.useTiptapEditor = useEditor;
88
-
89
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","names":["createSignal","Editor"],"sources":["../src/Editor.ts"],"sourcesContent":["import type { EditorOptions, JSONContent } from '@tiptap/core';\nimport { Editor } from '@tiptap/core';\nimport { createEffect, createSignal, onCleanup } from 'solid-js';\n\nexport type EditorRef = Editor | ((editor: Editor) => void);\n\nexport type BaseEditorOptions = Omit<Partial<EditorOptions>, 'element'>;\n\nexport interface UseEditorOptions<T extends HTMLElement> extends BaseEditorOptions {\n element: T;\n}\n\n/**\n * Reads a value from the editor and reads it again after every transaction.\n * `read` also receives `undefined` while `instance` returns no editor.\n */\nexport function createEditorTransaction<T, V extends Editor | undefined>(\n instance: () => V,\n read: (value: V) => T,\n): () => T {\n const [depend, update] = createSignal(undefined, { equals: false });\n\n function forceUpdate(): void {\n update();\n }\n\n createEffect(() => {\n const editor = instance();\n if (editor) {\n editor.on('transaction', forceUpdate);\n onCleanup(() => {\n editor.off('transaction', forceUpdate);\n });\n }\n });\n\n return () => {\n depend();\n return read(instance());\n };\n}\n\n/**\n * Creates a Tiptap editor from the given options.\n * The editor is destroyed and created again when a signal read by `props` changes.\n * It is also destroyed when the owner is disposed.\n */\nexport default function useEditor<T extends HTMLElement>(\n props: () => UseEditorOptions<T>,\n): () => Editor | undefined {\n const [signal, setSignal] = createSignal<Editor>();\n\n createEffect(() => {\n const instance = new Editor({\n ...props(),\n });\n\n onCleanup(() => {\n instance.destroy();\n });\n\n setSignal(instance);\n });\n\n return signal;\n}\n\n/** Returns the editor content as HTML. */\nexport function useEditorHTML(editor: () => Editor | undefined): () => string | undefined {\n return createEditorTransaction(editor, (instance) => instance?.getHTML());\n}\n\n/** Returns the editor content as Tiptap JSON. */\nexport function useEditorJSON(editor: () => Editor | undefined): () => JSONContent | undefined {\n return createEditorTransaction(editor, (instance) => instance?.getJSON());\n}\n\n/**\n * Returns whether the node or mark at the selection is active.\n * Pass a node or mark name with optional attributes, or only the attributes.\n */\nexport function useEditorIsActive(\n editor: () => Editor | undefined,\n ...args:\n | [name: () => string, options?: Record<string, unknown>]\n | [options: Record<string, unknown>]\n): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => {\n if (args.length === 2) {\n return instance?.isActive(args[0](), args[1]);\n }\n const [first] = args;\n if (typeof first === 'function') {\n return instance?.isActive(first());\n }\n return instance?.isActive(first);\n });\n}\n\n/** Returns whether the editor has no content. */\nexport function useEditorIsEmpty(editor: () => Editor | undefined): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => instance?.isEmpty);\n}\n\n/** Returns whether the editor accepts input. */\nexport function useEditorIsEditable(editor: () => Editor | undefined): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => instance?.isEditable);\n}\n\n/** Returns whether the editor has focus. */\nexport function useEditorIsFocused(editor: () => Editor | undefined): () => boolean | undefined {\n return createEditorTransaction(editor, (instance) => instance?.isFocused);\n}\n"],"mappings":";;;;;;;;AAgBA,SAAgB,wBACd,UACA,MACS;CACT,MAAM,CAAC,QAAQ,WAAA,GAAUA,SAAAA,aAAAA,CAAa,KAAA,GAAW,EAAE,QAAQ,MAAM,CAAC;CAElE,SAAS,cAAoB;EAC3B,OAAO;CACT;CAEA,CAAA,GAAA,SAAA,aAAA,OAAmB;EACjB,MAAM,SAAS,SAAS;EACxB,IAAI,QAAQ;GACV,OAAO,GAAG,eAAe,WAAW;GACpC,CAAA,GAAA,SAAA,UAAA,OAAgB;IACd,OAAO,IAAI,eAAe,WAAW;GACvC,CAAC;EACH;CACF,CAAC;CAED,aAAa;EACX,OAAO;EACP,OAAO,KAAK,SAAS,CAAC;CACxB;AACF;;;;;;AAOA,SAAwB,UACtB,OAC0B;CAC1B,MAAM,CAAC,QAAQ,cAAA,GAAaA,SAAAA,aAAAA,CAAqB;CAEjD,CAAA,GAAA,SAAA,aAAA,OAAmB;EACjB,MAAM,WAAW,IAAIC,aAAAA,OAAO,EAC1B,GAAG,MAAM,EACX,CAAC;EAED,CAAA,GAAA,SAAA,UAAA,OAAgB;GACd,SAAS,QAAQ;EACnB,CAAC;EAED,UAAU,QAAQ;CACpB,CAAC;CAED,OAAO;AACT;;AAGA,SAAgB,cAAc,QAA4D;CACxF,OAAO,wBAAwB,SAAS,aAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAa,SAAU,QAAQ,CAAC;AAC1E;;AAGA,SAAgB,cAAc,QAAiE;CAC7F,OAAO,wBAAwB,SAAS,aAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAa,SAAU,QAAQ,CAAC;AAC1E;;;;;AAMA,SAAgB,kBACd,QACA,GAAG,MAGwB;CAC3B,OAAO,wBAAwB,SAAS,aAAa;EACnD,IAAI,KAAK,WAAW,GAClB,OAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAO,SAAU,SAAS,KAAK,EAAE,CAAC,GAAG,KAAK,EAAE;EAE9C,MAAM,CAAC,SAAS;EAChB,IAAI,OAAO,UAAU,YACnB,OAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAO,SAAU,SAAS,MAAM,CAAC;EAEnC,OAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAO,SAAU,SAAS,KAAK;CACjC,CAAC;AACH;;AAGA,SAAgB,iBAAiB,QAA6D;CAC5F,OAAO,wBAAwB,SAAS,aAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAa,SAAU,OAAO;AACxE;;AAGA,SAAgB,oBAAoB,QAA6D;CAC/F,OAAO,wBAAwB,SAAS,aAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAa,SAAU,UAAU;AAC3E;;AAGA,SAAgB,mBAAmB,QAA6D;CAC9F,OAAO,wBAAwB,SAAS,aAAA,aAAA,QAAA,aAAA,KAAA,IAAA,KAAA,IAAa,SAAU,SAAS;AAC1E"}
package/dist/index.d.cts DELETED
@@ -1,36 +0,0 @@
1
- import { Editor, EditorOptions, JSONContent } from "@tiptap/core";
2
- //#region src/Editor.d.ts
3
- type EditorRef = Editor | ((editor: Editor) => void);
4
- type BaseEditorOptions = Omit<Partial<EditorOptions>, 'element'>;
5
- interface UseEditorOptions<T extends HTMLElement> extends BaseEditorOptions {
6
- element: T;
7
- }
8
- /**
9
- * Reads a value from the editor and reads it again after every transaction.
10
- * `read` also receives `undefined` while `instance` returns no editor.
11
- */
12
- export declare function createEditorTransaction<T, V extends Editor | undefined>(instance: () => V, read: (value: V) => T): () => T;
13
- /**
14
- * Creates a Tiptap editor from the given options.
15
- * The editor is destroyed and created again when a signal read by `props` changes.
16
- * It is also destroyed when the owner is disposed.
17
- */
18
- declare function useEditor<T extends HTMLElement>(props: () => UseEditorOptions<T>): () => Editor | undefined;
19
- /** Returns the editor content as HTML. */
20
- export declare function useEditorHTML(editor: () => Editor | undefined): () => string | undefined;
21
- /** Returns the editor content as Tiptap JSON. */
22
- export declare function useEditorJSON(editor: () => Editor | undefined): () => JSONContent | undefined;
23
- /**
24
- * Returns whether the node or mark at the selection is active.
25
- * Pass a node or mark name with optional attributes, or only the attributes.
26
- */
27
- export declare function useEditorIsActive(editor: () => Editor | undefined, ...args: [name: () => string, options?: Record<string, unknown>] | [options: Record<string, unknown>]): () => boolean | undefined;
28
- /** Returns whether the editor has no content. */
29
- export declare function useEditorIsEmpty(editor: () => Editor | undefined): () => boolean | undefined;
30
- /** Returns whether the editor accepts input. */
31
- export declare function useEditorIsEditable(editor: () => Editor | undefined): () => boolean | undefined;
32
- /** Returns whether the editor has focus. */
33
- export declare function useEditorIsFocused(editor: () => Editor | undefined): () => boolean | undefined;
34
- //#endregion
35
- export { type BaseEditorOptions, type EditorRef, type UseEditorOptions, useEditor as createEditor, useEditor as createTiptapEditor, useEditor, useEditor as useTiptapEditor };
36
- //# sourceMappingURL=index.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../src/Editor.ts"],"mappings":";;KAIY,YAAY,WAAW,QAAQ;KAE/B,oBAAoB,KAAK,QAAQ;UAE5B,iBAAiB,UAAU,qBAAqB;EAC/D,SAAS;;;;;;wBAOK,wBAAwB,GAAG,UAAU,oBACnD,gBAAgB,GAChB,OAAO,OAAO,MAAM,UACb;;;;;;iBA4Be,UAAU,UAAU,aAC1C,aAAa,iBAAiB,WACvB;;wBAmBO,cAAc,cAAc;;wBAK5B,cAAc,cAAc,2BAA2B;;;;;wBAQvD,kBACd,cAAc,uBACX,OACE,oBAAoB,UAAU,4BAC9B,SAAS;;wBAeA,iBAAiB,cAAc;;wBAK/B,oBAAoB,cAAc;;wBAKlC,mBAAmB,cAAc"}