editate 0.6.27 → 0.7.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
@@ -101,6 +101,8 @@ import { useState, useEffect, useRef, useMemo } from "react";
101
101
  import {
102
102
  createEditor,
103
103
  plainTransferPlugin,
104
+ nodeIdPlugin,
105
+ NodeId,
104
106
  ToggleFormat,
105
107
  ToggleBlockAttr,
106
108
  } from "editate";
@@ -142,7 +144,9 @@ export const App = () => {
142
144
  const e = createEditor({
143
145
  doc,
144
146
  schema,
145
- }).exec(plainTransferPlugin);
147
+ })
148
+ .exec(plainTransferPlugin)
149
+ .exec(nodeIdPlugin);
146
150
  e.on("change", () => {
147
151
  setDoc(e.doc);
148
152
  });
@@ -186,11 +190,11 @@ export const App = () => {
186
190
  padding: 8,
187
191
  }}
188
192
  >
189
- {doc.children.map((b, i) => (
190
- <div key={i} style={{ textAlign: b.align }}>
191
- {b.children.map((n, j) => (
193
+ {doc.children.map((b) => (
194
+ <div key={NodeId(editor, b)} style={{ textAlign: b.align }}>
195
+ {b.children.map((n) => (
192
196
  <span
193
- key={j}
197
+ key={NodeId(editor, n)}
194
198
  style={{
195
199
  fontWeight: n.bold ? "bold" : undefined,
196
200
  fontStyle: n.italic ? "italic" : undefined,
@@ -209,15 +213,16 @@ export const App = () => {
209
213
 
210
214
  ### Other examples
211
215
 
212
- - React ([Demo](https://inokawa.github.io/editate/react), [Source](./examples/react))
213
- - Vue ([Demo](https://inokawa.github.io/editate/vue), [Source](./examples/vue))
214
- - Svelte ([Demo](https://inokawa.github.io/editate/svelte), [Source](./examples/svelte))
215
- - Solid ([Demo](https://inokawa.github.io/editate/solid), [Source](./examples/solid))
216
- - Angular ([Demo](https://inokawa.github.io/editate/angular), [Source](./examples/angular))
217
- - Preact ([Demo](https://inokawa.github.io/editate/preact), [Source](./examples/preact))
218
- - Alpine ([Demo](https://inokawa.github.io/editate/alpine), [Source](./examples/alpine))
219
- - Marko ([Demo](https://inokawa.github.io/editate/marko), [Source](./examples/marko))
220
- - Octane ([Demo](https://inokawa.github.io/editate/octane), [Source](./examples/octane))
216
+ - [React](https://react.dev) ([Demo](https://inokawa.github.io/editate/react), [Source](./examples/react))
217
+ - [Vue](https://vuejs.org) ([Demo](https://inokawa.github.io/editate/vue), [Source](./examples/vue))
218
+ - [Svelte](https://svelte.dev) ([Demo](https://inokawa.github.io/editate/svelte), [Source](./examples/svelte))
219
+ - [Solid](https://solidjs.com) ([Demo](https://inokawa.github.io/editate/solid), [Source](./examples/solid))
220
+ - [Angular](https://angular.dev) ([Demo](https://inokawa.github.io/editate/angular), [Source](./examples/angular))
221
+ - [Preact](https://preactjs.com) ([Demo](https://inokawa.github.io/editate/preact), [Source](./examples/preact))
222
+ - [Alpine](https://alpinejs.dev) ([Demo](https://inokawa.github.io/editate/alpine), [Source](./examples/alpine))
223
+ - [Marko](https://markojs.com) ([Demo](https://inokawa.github.io/editate/marko), [Source](./examples/marko))
224
+ - [Octane](https://octanejs.dev) ([Demo](https://inokawa.github.io/editate/octane), [Source](./examples/octane))
225
+ - [Remix](https://remix.run) ([Demo](https://inokawa.github.io/editate/remix), [Source](./examples/remix))
221
226
  - Vanilla ([Demo](https://inokawa.github.io/editate/vanilla), [Source](./examples/vanilla))
222
227
 
223
228
  ...and more! Contribution welcome!
package/lib/commands.d.ts CHANGED
@@ -33,13 +33,13 @@ export declare function Format<T extends DocNode, N extends Omit<InferTextNode<T
33
33
  */
34
34
  export declare function ToggleFormat<T extends DocNode>(editor: Editor<T>, key: Extract<ToggleableKey<Omit<InferTextNode<T>, "text">>, string>, range?: Range): void;
35
35
  /**
36
- * Set attr to a block node at the caret or specified position.
36
+ * Set attr to block nodes in the selection or specified range.
37
37
  */
38
- export declare function SetBlockAttr<T extends DocNode, N extends InferLeafBlockNode<T>, K extends string>(editor: Editor<T>, key: K, value: ExtractAttrValue<N, K>, offset?: number): void;
38
+ export declare function SetBlockAttr<T extends DocNode, N extends InferLeafBlockNode<T>, K extends string>(editor: Editor<T>, key: K, value: ExtractAttrValue<N, K>, range?: Range): void;
39
39
  /**
40
- * Toggle attr of block node at the caret or specified position.
40
+ * Toggle attr of block nodes in the selection or specified range.
41
41
  */
42
- export declare function ToggleBlockAttr<T extends DocNode, N extends InferLeafBlockNode<T>, K extends string>(editor: Editor<T>, key: K, onValue: ExtractAttrValue<N, K>, offValue: ExtractAttrValue<N, K>, offset?: number): void;
42
+ export declare function ToggleBlockAttr<T extends DocNode, N extends InferLeafBlockNode<T>, K extends string>(editor: Editor<T>, key: K, onValue: ExtractAttrValue<N, K>, offValue: ExtractAttrValue<N, K>, range?: Range): void;
43
43
  /**
44
44
  * Set attr to a void node at the caret or specified position.
45
45
  */
package/lib/doc/node.d.ts CHANGED
@@ -6,6 +6,14 @@ export declare const getLeafBlockAt: <T extends DocNode | BlockNode>(node: T, of
6
6
  export declare const getLeafAt: <T extends DocNode | BlockNode>(node: T, offset: number, isBackwardAffinity?: boolean) => [node: InferInlineNode<T>, offset: number, path: Path] | null;
7
7
  export declare const getNodeOffset: (root: DocNode | BlockNode, target: Node) => number | null;
8
8
  export declare const offsetToPosition: (node: DocNode | BlockNode, offset: number) => DomPosition;
9
- export declare function iterLeafBlocks<T extends Node>(node: T, range: Range): Generator<[node: InferLeafBlockNode<T>, offset: number], void, void>;
10
- export declare function iterLeaves<T extends Node>(node: T, range: Range): Generator<[node: InferInlineNode<T>, offset: number], void, void>;
9
+ export declare function iterLeafBlocks<T extends Node>(node: T, range: Range): Generator<[
10
+ node: InferLeafBlockNode<T>,
11
+ offset: number,
12
+ range: Range
13
+ ], void, void>;
14
+ export declare function iterLeaves<T extends Node>(node: T, range: Range): Generator<[
15
+ node: InferInlineNode<T>,
16
+ offset: number,
17
+ range: Range
18
+ ], void, void>;
11
19
  export declare const sliceText: <T extends Node>(node: T, start?: number, end?: number, voidToString?: (node: InferVoidNode<T>) => string) => string;
@@ -1,4 +1,4 @@
1
- import { Fragment, Path, Range } from './types.js';
1
+ import { Fragment, Range } from './types.js';
2
2
  declare const OP_DELETE = "delete";
3
3
  type DeleteOperation = Readonly<{
4
4
  type: typeof OP_DELETE;
@@ -20,17 +20,14 @@ declare const OP_FORMAT = "format";
20
20
  type FormatOperation = Readonly<{
21
21
  type: typeof OP_FORMAT;
22
22
  range: Range;
23
+ /** Must not be `text`, nor `children` unless the depth is 0. */
23
24
  key: string;
24
- value: unknown;
25
+ /** Omit or pass undefined to remove the attr. */
26
+ value?: unknown;
27
+ /** Depth of nodes to update like array.at() on the path from the root to each leaf in the range: -1 (default) is the leaf, -2 its block and 0 the root. */
28
+ depth?: number;
25
29
  }>;
26
- declare const OP_PATCH_NODE = "patch_node";
27
- type PatchNodeOperation = Readonly<{
28
- type: typeof OP_PATCH_NODE;
29
- path: Path;
30
- key: string;
31
- value: unknown;
32
- }>;
33
- export type Operation = DeleteOperation | InsertTextOperation | InsertNodeOperation | FormatOperation | PatchNodeOperation;
30
+ export type Operation = DeleteOperation | InsertTextOperation | InsertNodeOperation | FormatOperation;
34
31
  /**
35
32
  * Remap a position through the given operation.
36
33
  * @param stickBefore `true` to keep the position in place when content is inserted at it, instead of moving it after the inserted content.
package/lib/editor.d.ts CHANGED
@@ -61,10 +61,12 @@ export type CopyHook = (dataTransfer: DataTransfer) => void;
61
61
  export type PasteHook = (dataTransfer: DataTransfer) => string | Fragment | true | null;
62
62
  type EditorHookMap = {
63
63
  /**
64
- * Call `next(op)` to continue applying the operation, or `next()` with a nullish value to cancel it.
64
+ * Call `next(op)` to continue applying the operation, or `next()` with a nullish value to cancel it with its transaction.
65
+ * `next` returns the operation applied to the document after the following hooks, or null if it was cancelled or failed to apply. The transaction may still be discarded afterwards.
66
+ *
65
67
  * If the hook returns without calling `next`, the operation is passed through as is.
66
68
  */
67
- apply: (op: Operation, next: (op?: Operation | null) => void) => void;
69
+ apply: (op: Operation, next: (op?: Operation | null) => Operation | null) => void;
68
70
  mount: (element: HTMLElement, parser: Parser) => void | (() => void);
69
71
  keyboard: KeyboardHook;
70
72
  copy: CopyHook;
@@ -82,10 +84,10 @@ export interface Editor<T extends DocNode = DocNode> {
82
84
  */
83
85
  readonly: boolean;
84
86
  /**
85
- * Dispatches editing operations.
87
+ * Applies editing operations as a transaction, which is discarded if one of them is cancelled or throws, or the result is rejected by the schema.
86
88
  * @param op {@link Operation}
87
89
  */
88
- apply(op: Operation | Operation[]): this;
90
+ apply(op: Operation | readonly Operation[]): this;
89
91
  /**
90
92
  * Executes a function with editor bound as context.
91
93
  * @param fn {@link EditorCommandOrPlugin} or {@link EditorQuery}