@folijs/react 0.5.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.
@@ -1,12 +1,13 @@
1
- import {
2
- type Editor,
3
- type EditorDocument,
4
- type FoliDocument,
5
- type Plugin,
6
- type ReviewView,
7
- type Transaction,
8
- wordContext,
1
+ import type {
2
+ Editor,
3
+ EditorDocument,
4
+ Extension,
5
+ FoliDocument,
6
+ Plugin,
7
+ ReviewView,
8
+ Transaction,
9
9
  } from "@folijs/core";
10
+ import { documentJSON, wordContext } from "@folijs/core/internal";
10
11
  import { forwardRef, type ReactNode, useImperativeHandle, useRef } from "react";
11
12
  import {
12
13
  FoliEditorProvider,
@@ -16,7 +17,8 @@ import {
16
17
  } from "./editor.js";
17
18
  import { FoliCanvas, type FoliCanvasHandle, type FoliCanvasProps } from "./FoliCanvas.js";
18
19
  import { FoliComments } from "./FoliComments.js";
19
- import { FoliToolbar } from "./FoliToolbar.js";
20
+ import { FoliNavigation } from "./FoliNavigation.js";
21
+ import { FoliToolbar, type ToolbarItem } from "./FoliToolbar.js";
20
22
 
21
23
  /**
22
24
  * A change to the document's content, as `FoliEditor` reports it. `document` is a getter:
@@ -39,8 +41,17 @@ export interface FoliEditorProps extends Omit<FoliCanvasProps, "editor"> {
39
41
  * schema v3). To load another document later: `editor.setDocument()` or a new `key`.
40
42
  */
41
43
  initialContent?: EditorDocument | undefined;
42
- /** Read once on mount. */
44
+ /**
45
+ * Read once on mount: a different list later is ignored, with a warning unless
46
+ * `process.env.NODE_ENV` is `"production"` at run time (`useFoliEditor`). To change them, give
47
+ * `FoliEditor` a new `key`.
48
+ */
43
49
  plugins?: readonly Plugin[] | undefined;
50
+ /**
51
+ * Read once on mount, as `plugins`: custom nodes and marks with their view hooks
52
+ * (`ViewExtension`).
53
+ */
54
+ extensions?: readonly Extension[] | undefined;
44
55
  /** Read once on mount: who edits (tracked changes are made in this name). */
45
56
  author?: string | undefined;
46
57
  /** Read once on mount: record edits as tracked changes (the toolbar turns it on and off). */
@@ -53,8 +64,11 @@ export interface FoliEditorProps extends Omit<FoliCanvasProps, "editor"> {
53
64
  * caller already has the document.
54
65
  */
55
66
  onChange?: ((change: FoliChange) => void) | undefined;
56
- /** Show the default `FoliToolbar` above the pages (default true). */
57
- toolbar?: boolean | undefined;
67
+ /**
68
+ * Show `FoliToolbar` above the pages (default true): with `defaultToolbarItems`, or with these
69
+ * groups of items (`FoliToolbarProps.items`).
70
+ */
71
+ toolbar?: boolean | readonly (readonly ToolbarItem[])[] | undefined;
58
72
  /**
59
73
  * Show the comments pane (`FoliComments`) beside the pages: `"auto"` (the default) while the
60
74
  * document has comments, `true` always, `false` never.
@@ -78,6 +92,7 @@ export const FoliEditor = forwardRef<FoliEditorHandle, FoliEditorProps>(function
78
92
  {
79
93
  initialContent,
80
94
  plugins,
95
+ extensions,
81
96
  author,
82
97
  trackChanges,
83
98
  reviewView,
@@ -89,7 +104,14 @@ export const FoliEditor = forwardRef<FoliEditorHandle, FoliEditorProps>(function
89
104
  },
90
105
  ref,
91
106
  ) {
92
- const editor = useFoliEditor({ initialContent, plugins, author, trackChanges, reviewView });
107
+ const editor = useFoliEditor({
108
+ initialContent,
109
+ plugins,
110
+ extensions,
111
+ author,
112
+ trackChanges,
113
+ reviewView,
114
+ });
93
115
  const hasComments = useEditorState(
94
116
  (state) => (wordContext(state)?.envelope.comments.length ?? 0) > 0,
95
117
  { editor },
@@ -114,10 +136,10 @@ export const FoliEditor = forwardRef<FoliEditorHandle, FoliEditorProps>(function
114
136
  editor,
115
137
  transactions,
116
138
  get document() {
117
- document ??= {
118
- ...(structuredClone(wordContext(state)?.envelope) as Omit<FoliDocument, "content">),
119
- content: state.doc.toJSON(),
120
- };
139
+ const envelope = wordContext(state)?.envelope;
140
+ document ??= envelope
141
+ ? documentJSON(envelope, state.doc)
142
+ : ({ content: state.doc.toJSON() } as FoliDocument);
121
143
  return document;
122
144
  },
123
145
  });
@@ -133,11 +155,13 @@ export const FoliEditor = forwardRef<FoliEditorHandle, FoliEditorProps>(function
133
155
  disabled={canvas.readOnly}
134
156
  comments={comments !== false}
135
157
  upload={canvas.upload}
158
+ items={toolbar === true ? undefined : toolbar}
136
159
  onClickCommand={() => canvasRef.current?.focus()}
137
160
  />
138
161
  )}
139
162
  {children}
140
163
  <div className="foli-editor-body">
164
+ <FoliNavigation editor={editor} readOnly={canvas.readOnly} />
141
165
  <FoliCanvas ref={canvasRef} editor={editor} {...canvas} />
142
166
  {/* "auto": mounted while hidden, so a comment made from the toolbar opens for writing. */}
143
167
  {comments !== false && (