scriviamo-react 0.0.0-stage → 1.0.1-beta.16
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/LICENSE.md +21 -0
- package/README.md +79 -2
- package/dist/editorbinding.d.ts +67 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +211 -0
- package/dist/inspector.d.ts +12 -0
- package/dist/inspector.js +41 -0
- package/dist/inspectorbinding.d.ts +13 -0
- package/package.json +45 -4
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 GraffitiSuite Solutions
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Scriviamo for React
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React components for the Scriviamo editor: `ScriviamoEditor` and `ScriviamoInspector`.
|
|
4
|
+
|
|
5
|
+
```tsx
|
|
6
|
+
import {useState} from "react"
|
|
7
|
+
import type {Editor} from "scriviamo"
|
|
8
|
+
import {ScriviamoEditor} from "scriviamo-react"
|
|
9
|
+
import {ScriviamoInspector} from "scriviamo-react/inspector"
|
|
10
|
+
|
|
11
|
+
export function NotesPage() {
|
|
12
|
+
const [doc, setDoc] = useState("# Notes\n\nStart writing.")
|
|
13
|
+
const [editor, setEditor] = useState<Editor | null>(null)
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
<div style={{display: "flex", gap: 8}}>
|
|
17
|
+
<ScriviamoEditor
|
|
18
|
+
value={doc}
|
|
19
|
+
format="markdown"
|
|
20
|
+
onChange={setDoc}
|
|
21
|
+
onReady={setEditor}
|
|
22
|
+
style={{flex: 1, height: 500}}
|
|
23
|
+
/>
|
|
24
|
+
<ScriviamoInspector editor={editor} style={{width: 320}} />
|
|
25
|
+
</div>
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Load Bootstrap 5's CSS and Bootstrap Icons, which Scriviamo's interface uses, then Scriviamo's
|
|
31
|
+
stylesheet, `dist/scriviamo.css`, and those of any add-ons, as you load other CSS. Bootstrap's
|
|
32
|
+
JavaScript isn't needed.
|
|
33
|
+
|
|
34
|
+
## ScriviamoEditor
|
|
35
|
+
|
|
36
|
+
Takes every setting in the [wrappers README](../README.md#settings), plus:
|
|
37
|
+
|
|
38
|
+
| Prop | Meaning |
|
|
39
|
+
|---|---|
|
|
40
|
+
| `onChange(value, editor)` | The document changed. `value` is in `format`. |
|
|
41
|
+
| `onSelectionChange(node, editor)` | The primary selected node changed |
|
|
42
|
+
| `onReady(editor)` | The editor was created |
|
|
43
|
+
| `className`, `style` | For the element the editor fills. Give it a height, or set `adaptiveHeight`. |
|
|
44
|
+
|
|
45
|
+
`value` can be left out for an editor that manages its own document: read it with the editor's
|
|
46
|
+
`toJSON()` when you need it.
|
|
47
|
+
|
|
48
|
+
A ref gives the editor:
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
const ref = useRef<ScriviamoEditorHandle>(null)
|
|
52
|
+
// ...
|
|
53
|
+
<ScriviamoEditor ref={ref} />
|
|
54
|
+
// ...
|
|
55
|
+
ref.current?.editor?.print()
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The editor is created when the component mounts and destroyed when it unmounts. Props are
|
|
59
|
+
applied as they change; only `value` changing from outside reloads the document.
|
|
60
|
+
|
|
61
|
+
## ScriviamoInspector
|
|
62
|
+
|
|
63
|
+
Imported from `scriviamo-react/inspector`. The inspector component needs Scriviamo's `inspector` add-on, which is premium, so it has its own entry point. Import the add-on before showing the inspector: `scriviamo/addons/inspector` from this repository, or `@scriviamo-pro/pro/addons/inspector` (or `pro-collab`) from npm, with its stylesheet. The component finds it whichever package it came from (see [The inspector](../../docs/inspector.md)); the editor component works without it.
|
|
64
|
+
|
|
65
|
+
| Prop | Meaning |
|
|
66
|
+
|---|---|
|
|
67
|
+
| `editor` | The editor to follow, such as the one `onReady` gives |
|
|
68
|
+
| `collapsedSections` | Identifiers of sections that start collapsed |
|
|
69
|
+
| `className`, `style` | For the inspector's element |
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## Building
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
npm install
|
|
76
|
+
npm run build
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The build writes `dist/index.js` and its declarations. React and Scriviamo stay external, as peer
|
|
80
|
+
dependencies.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { AutosaveFunction, Editor, FileUploader, Node, ToolbarDisplayModes } from "scriviamo";
|
|
2
|
+
/** The document format a wrapper's value is read and written in. */
|
|
3
|
+
export type ScriviamoFormat = "json" | "html" | "markdown";
|
|
4
|
+
/** Settings every framework wrapper accepts. Missing settings keep the editor's defaults. */
|
|
5
|
+
export interface ScriviamoSettings {
|
|
6
|
+
/** The document, in format. Changing it from outside replaces the document. */
|
|
7
|
+
value?: string;
|
|
8
|
+
/** The format of value. "json" keeps everything; HTML and Markdown keep what they can express. */
|
|
9
|
+
format?: ScriviamoFormat;
|
|
10
|
+
readOnly?: boolean;
|
|
11
|
+
toolbarVisible?: boolean;
|
|
12
|
+
toolbarDisplayMode?: ToolbarDisplayModes;
|
|
13
|
+
showContextualToolbars?: boolean;
|
|
14
|
+
hoverInsertion?: boolean;
|
|
15
|
+
markdownShortcuts?: boolean;
|
|
16
|
+
allowAnnotations?: boolean;
|
|
17
|
+
adaptiveHeight?: boolean;
|
|
18
|
+
fontName?: string;
|
|
19
|
+
fontSize?: number;
|
|
20
|
+
/** A locale pack, or an object of localized strings. */
|
|
21
|
+
locale?: string | object;
|
|
22
|
+
/** Extra classes for the editor element. */
|
|
23
|
+
editorClass?: string;
|
|
24
|
+
/** Shows the status bar: word and character counts, reading time and the autosave state. */
|
|
25
|
+
statusBar?: boolean;
|
|
26
|
+
/** Saves the document after changes: a save function, or one with its settings. */
|
|
27
|
+
autosave?: AutosaveFunction | {
|
|
28
|
+
save: AutosaveFunction;
|
|
29
|
+
delay?: number;
|
|
30
|
+
maximumWait?: number;
|
|
31
|
+
retryDelay?: number;
|
|
32
|
+
warnBeforeLeaving?: boolean;
|
|
33
|
+
} | null;
|
|
34
|
+
/** Uploads the files people add, such as pictures, and returns their addresses. */
|
|
35
|
+
fileUploader?: FileUploader | null;
|
|
36
|
+
/** Milliseconds to wait after typing stops before reporting a change; 0 reports each change. */
|
|
37
|
+
changeDelay?: number;
|
|
38
|
+
/** Editor events to listen to, by name, such as {linkPressed: event => ...}. */
|
|
39
|
+
events?: {
|
|
40
|
+
[name: string]: (event: CustomEvent) => void;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** What a binding reports to its framework wrapper. */
|
|
44
|
+
export interface ScriviamoCallbacks {
|
|
45
|
+
/** The document changed in the editor. value is in the binding's format. */
|
|
46
|
+
change?(value: string, editor: Editor): void;
|
|
47
|
+
/** The primary selected node changed. */
|
|
48
|
+
selectionChange?(node: Node | null, editor: Editor): void;
|
|
49
|
+
/** The editor was created. */
|
|
50
|
+
ready?(editor: Editor): void;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Keeps an editor in step with a framework component's settings: it creates the editor, applies
|
|
54
|
+
* changed settings to it, reports changes, and tears it down. Each framework wrapper is a thin
|
|
55
|
+
* layer over this.
|
|
56
|
+
*/
|
|
57
|
+
export declare class EditorBinding {
|
|
58
|
+
#private;
|
|
59
|
+
readonly editor: Editor;
|
|
60
|
+
constructor(element: HTMLElement, settings: ScriviamoSettings, callbacks?: ScriviamoCallbacks);
|
|
61
|
+
/** Applies new settings from the framework component. */
|
|
62
|
+
update(settings: ScriviamoSettings): void;
|
|
63
|
+
/** The document in the binding's format. */
|
|
64
|
+
get value(): string;
|
|
65
|
+
/** Reports any waiting change, then removes the editor. */
|
|
66
|
+
destroy(): void;
|
|
67
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type CSSProperties } from "react";
|
|
2
|
+
import type { Editor, Node } from "scriviamo";
|
|
3
|
+
import { type ScriviamoSettings } from "./editorbinding";
|
|
4
|
+
export type { ScriviamoFormat, ScriviamoSettings } from "./editorbinding";
|
|
5
|
+
export interface ScriviamoEditorProps extends ScriviamoSettings {
|
|
6
|
+
/** The document changed. value is in format. */
|
|
7
|
+
onChange?: (value: string, editor: Editor) => void;
|
|
8
|
+
/** The primary selected node changed. */
|
|
9
|
+
onSelectionChange?: (node: Node | null, editor: Editor) => void;
|
|
10
|
+
/** The editor was created. */
|
|
11
|
+
onReady?: (editor: Editor) => void;
|
|
12
|
+
/** Classes and style for the element the editor fills. Give it a height, or set adaptiveHeight. */
|
|
13
|
+
className?: string;
|
|
14
|
+
style?: CSSProperties;
|
|
15
|
+
}
|
|
16
|
+
/** What a ref to ScriviamoEditor gives: the editor, once it exists. */
|
|
17
|
+
export interface ScriviamoEditorHandle {
|
|
18
|
+
readonly editor: Editor | null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A Scriviamo editor. Import the add-ons you use before rendering it.
|
|
22
|
+
*
|
|
23
|
+
* <ScriviamoEditor value={doc} onChange={setDoc} style={{height: 500}} />
|
|
24
|
+
*
|
|
25
|
+
* value is read when it changes from outside: the editor's own changes come back through
|
|
26
|
+
* onChange, and passing them back in doesn't reload the document.
|
|
27
|
+
*/
|
|
28
|
+
export declare const ScriviamoEditor: import("react").ForwardRefExoticComponent<ScriviamoEditorProps & import("react").RefAttributes<ScriviamoEditorHandle>>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// src/index.tsx
|
|
2
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
3
|
+
|
|
4
|
+
// src/editorbinding.ts
|
|
5
|
+
import { Editor } from "scriviamo";
|
|
6
|
+
var EditorBinding = class _EditorBinding {
|
|
7
|
+
editor;
|
|
8
|
+
#settings;
|
|
9
|
+
#callbacks;
|
|
10
|
+
/** The value last reported, and the value last loaded from outside, so a wrapper passing either back doesn't reload the document. */
|
|
11
|
+
#value;
|
|
12
|
+
#loadedValue;
|
|
13
|
+
/** True while a value from outside is loading, whose changes aren't the user's. */
|
|
14
|
+
#loading = false;
|
|
15
|
+
#timer = null;
|
|
16
|
+
#listeners = [];
|
|
17
|
+
#onContent;
|
|
18
|
+
#onSelection;
|
|
19
|
+
constructor(element, settings, callbacks = {}) {
|
|
20
|
+
this.#settings = { ...settings };
|
|
21
|
+
this.#callbacks = callbacks;
|
|
22
|
+
this.editor = new Editor(element, _EditorBinding.#options(settings));
|
|
23
|
+
if (settings.value !== void 0) this.#load(settings.value, this.#format(settings));
|
|
24
|
+
this.#onContent = () => {
|
|
25
|
+
if (!this.#loading) this.#scheduleChange();
|
|
26
|
+
};
|
|
27
|
+
this.#onSelection = (event) => this.#callbacks.selectionChange?.(event.detail?.node ?? null, this.editor);
|
|
28
|
+
this.editor.addEventListener("contentChanged", this.#onContent);
|
|
29
|
+
this.editor.addEventListener("selectionChanged", this.#onSelection);
|
|
30
|
+
this.#listen(settings.events);
|
|
31
|
+
callbacks.ready?.(this.editor);
|
|
32
|
+
}
|
|
33
|
+
/** The options a new editor starts with. Content is loaded separately, in the chosen format. */
|
|
34
|
+
static #options(settings) {
|
|
35
|
+
let options = {};
|
|
36
|
+
if (settings.readOnly !== void 0) options.readOnly = settings.readOnly;
|
|
37
|
+
if (settings.toolbarVisible !== void 0) options.toolbarVisible = settings.toolbarVisible;
|
|
38
|
+
if (settings.toolbarDisplayMode !== void 0) options.toolbarDisplayMode = settings.toolbarDisplayMode;
|
|
39
|
+
if (settings.showContextualToolbars !== void 0) options.showContextualToolbars = settings.showContextualToolbars;
|
|
40
|
+
if (settings.hoverInsertion !== void 0) options.isHoverInsertionEnabled = settings.hoverInsertion;
|
|
41
|
+
if (settings.markdownShortcuts !== void 0) options.markdownShortcuts = settings.markdownShortcuts;
|
|
42
|
+
if (settings.allowAnnotations !== void 0) options.allowAnnotations = settings.allowAnnotations;
|
|
43
|
+
if (settings.adaptiveHeight !== void 0) options.adaptiveHeight = settings.adaptiveHeight;
|
|
44
|
+
if (settings.fontName) options.fontName = settings.fontName;
|
|
45
|
+
if (settings.fontSize) options.fontSize = settings.fontSize;
|
|
46
|
+
if (settings.locale) options.strings = settings.locale;
|
|
47
|
+
if (settings.editorClass) options.class = settings.editorClass;
|
|
48
|
+
if (settings.statusBar !== void 0) options.statusBar = settings.statusBar;
|
|
49
|
+
if (settings.autosave) options.autosave = settings.autosave;
|
|
50
|
+
if (settings.fileUploader) options.fileUploader = settings.fileUploader;
|
|
51
|
+
return options;
|
|
52
|
+
}
|
|
53
|
+
#format(settings) {
|
|
54
|
+
return settings.format == "html" || settings.format == "markdown" ? settings.format : "json";
|
|
55
|
+
}
|
|
56
|
+
/** Applies the settings that changed since previous. A new editor gets them as options instead. */
|
|
57
|
+
#apply(settings, previous) {
|
|
58
|
+
let editor = this.editor;
|
|
59
|
+
let changed = (name) => settings[name] !== void 0 && settings[name] !== previous[name];
|
|
60
|
+
if (changed("readOnly")) editor.readOnly = !!settings.readOnly;
|
|
61
|
+
if (changed("toolbarVisible")) editor.toolbarVisible = !!settings.toolbarVisible;
|
|
62
|
+
if (changed("toolbarDisplayMode")) editor.toolbarDisplayMode = settings.toolbarDisplayMode;
|
|
63
|
+
if (changed("showContextualToolbars")) editor.showContextualToolbars = !!settings.showContextualToolbars;
|
|
64
|
+
if (changed("hoverInsertion")) editor.isHoverInsertionEnabled = !!settings.hoverInsertion;
|
|
65
|
+
if (changed("markdownShortcuts")) editor.markdownShortcuts = !!settings.markdownShortcuts;
|
|
66
|
+
if (changed("allowAnnotations")) editor.allowAnnotations = !!settings.allowAnnotations;
|
|
67
|
+
if (changed("adaptiveHeight")) editor.adaptiveHeight = !!settings.adaptiveHeight;
|
|
68
|
+
if (changed("fontName")) editor.fontName = settings.fontName;
|
|
69
|
+
if (changed("fontSize")) editor.fontSize = settings.fontSize;
|
|
70
|
+
if (changed("editorClass")) editor.class = settings.editorClass;
|
|
71
|
+
if (changed("locale")) editor.localize(settings.locale);
|
|
72
|
+
if (changed("statusBar")) editor.statusBarVisible = !!settings.statusBar;
|
|
73
|
+
if (settings.autosave !== previous.autosave) editor.updateAutosave(settings.autosave ?? null);
|
|
74
|
+
if (settings.fileUploader !== previous.fileUploader) editor.fileUploader = settings.fileUploader ?? null;
|
|
75
|
+
}
|
|
76
|
+
/** Applies new settings from the framework component. */
|
|
77
|
+
update(settings) {
|
|
78
|
+
let previous = this.#settings;
|
|
79
|
+
this.#settings = { ...settings };
|
|
80
|
+
this.#apply(settings, previous);
|
|
81
|
+
if (settings.events !== previous.events) {
|
|
82
|
+
this.#unlisten();
|
|
83
|
+
this.#listen(settings.events);
|
|
84
|
+
}
|
|
85
|
+
let format = this.#format(settings);
|
|
86
|
+
let formatChanged = format != this.#format(previous);
|
|
87
|
+
let known = settings.value === this.#value || settings.value === this.#loadedValue;
|
|
88
|
+
if (settings.value !== void 0 && !known) {
|
|
89
|
+
this.#flush();
|
|
90
|
+
this.#load(settings.value, format);
|
|
91
|
+
} else if (formatChanged) {
|
|
92
|
+
this.#value = this.value;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** The document in the binding's format. */
|
|
96
|
+
get value() {
|
|
97
|
+
switch (this.#format(this.#settings)) {
|
|
98
|
+
case "html":
|
|
99
|
+
return this.editor.toHTML();
|
|
100
|
+
case "markdown":
|
|
101
|
+
return this.editor.toMarkdown();
|
|
102
|
+
}
|
|
103
|
+
return this.editor.toJSON();
|
|
104
|
+
}
|
|
105
|
+
#load(value, format) {
|
|
106
|
+
let editor = this.editor;
|
|
107
|
+
this.#loading = true;
|
|
108
|
+
try {
|
|
109
|
+
this.#loadInto(editor, value, format);
|
|
110
|
+
} finally {
|
|
111
|
+
this.#loading = false;
|
|
112
|
+
}
|
|
113
|
+
editor.history.reset();
|
|
114
|
+
editor.isChanged = false;
|
|
115
|
+
this.#loadedValue = value;
|
|
116
|
+
this.#value = this.value;
|
|
117
|
+
}
|
|
118
|
+
#loadInto(editor, value, format) {
|
|
119
|
+
if (format == "json") {
|
|
120
|
+
if (value.trim().length == 0) editor.removeAllNodes();
|
|
121
|
+
else editor.fromJSON(value);
|
|
122
|
+
} else if (format == "html") {
|
|
123
|
+
if (value.trim().length == 0) editor.removeAllNodes();
|
|
124
|
+
else editor.fromHTML(value);
|
|
125
|
+
} else {
|
|
126
|
+
if (value.trim().length == 0) editor.removeAllNodes();
|
|
127
|
+
else editor.fromMarkdown(value);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
#scheduleChange() {
|
|
131
|
+
let delay = Math.max(0, this.#settings.changeDelay ?? 0);
|
|
132
|
+
if (this.#timer) clearTimeout(this.#timer);
|
|
133
|
+
this.#timer = null;
|
|
134
|
+
if (delay == 0) {
|
|
135
|
+
this.#report();
|
|
136
|
+
} else {
|
|
137
|
+
this.#timer = setTimeout(() => {
|
|
138
|
+
this.#timer = null;
|
|
139
|
+
this.#report();
|
|
140
|
+
}, delay);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
#report() {
|
|
144
|
+
let value = this.value;
|
|
145
|
+
if (value === this.#value) return;
|
|
146
|
+
this.#value = value;
|
|
147
|
+
this.#loadedValue = void 0;
|
|
148
|
+
this.#callbacks.change?.(value, this.editor);
|
|
149
|
+
}
|
|
150
|
+
/** Reports a change that is waiting for changeDelay. */
|
|
151
|
+
#flush() {
|
|
152
|
+
if (!this.#timer) return;
|
|
153
|
+
clearTimeout(this.#timer);
|
|
154
|
+
this.#timer = null;
|
|
155
|
+
this.#report();
|
|
156
|
+
}
|
|
157
|
+
#listen(events) {
|
|
158
|
+
for (let name of Object.keys(events || {})) {
|
|
159
|
+
let handler = events[name];
|
|
160
|
+
if (typeof handler != "function") continue;
|
|
161
|
+
let listener = (event) => handler(event);
|
|
162
|
+
this.editor.addEventListener(name, listener);
|
|
163
|
+
this.#listeners.push([name, listener]);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
#unlisten() {
|
|
167
|
+
for (let [name, listener] of this.#listeners) this.editor.removeEventListener(name, listener);
|
|
168
|
+
this.#listeners = [];
|
|
169
|
+
}
|
|
170
|
+
/** Reports any waiting change, then removes the editor. */
|
|
171
|
+
destroy() {
|
|
172
|
+
this.#flush();
|
|
173
|
+
this.#unlisten();
|
|
174
|
+
this.editor.removeEventListener("contentChanged", this.#onContent);
|
|
175
|
+
this.editor.removeEventListener("selectionChanged", this.#onSelection);
|
|
176
|
+
this.editor.destroy();
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// src/index.tsx
|
|
181
|
+
import { jsx } from "react/jsx-runtime";
|
|
182
|
+
var ScriviamoEditor = forwardRef(function ScriviamoEditor2(props, ref) {
|
|
183
|
+
const host = useRef(null);
|
|
184
|
+
const binding = useRef(null);
|
|
185
|
+
const latest = useRef(props);
|
|
186
|
+
latest.current = props;
|
|
187
|
+
useEffect(() => {
|
|
188
|
+
let created = new EditorBinding(host.current, latest.current, {
|
|
189
|
+
change: (value, editor) => latest.current.onChange?.(value, editor),
|
|
190
|
+
selectionChange: (node, editor) => latest.current.onSelectionChange?.(node, editor),
|
|
191
|
+
ready: (editor) => latest.current.onReady?.(editor)
|
|
192
|
+
});
|
|
193
|
+
binding.current = created;
|
|
194
|
+
return () => {
|
|
195
|
+
created.destroy();
|
|
196
|
+
binding.current = null;
|
|
197
|
+
};
|
|
198
|
+
}, []);
|
|
199
|
+
useEffect(() => {
|
|
200
|
+
binding.current?.update(props);
|
|
201
|
+
});
|
|
202
|
+
useImperativeHandle(ref, () => ({
|
|
203
|
+
get editor() {
|
|
204
|
+
return binding.current ? binding.current.editor : null;
|
|
205
|
+
}
|
|
206
|
+
}), []);
|
|
207
|
+
return /* @__PURE__ */ jsx("div", { ref: host, className: props.className, style: props.style });
|
|
208
|
+
});
|
|
209
|
+
export {
|
|
210
|
+
ScriviamoEditor
|
|
211
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type CSSProperties } from "react";
|
|
2
|
+
import type { Editor } from "scriviamo";
|
|
3
|
+
export interface ScriviamoInspectorProps {
|
|
4
|
+
/** The editor to follow, such as the one onReady gives. */
|
|
5
|
+
editor: Editor | null;
|
|
6
|
+
/** Identifiers of sections that start collapsed. */
|
|
7
|
+
collapsedSections?: Array<string>;
|
|
8
|
+
className?: string;
|
|
9
|
+
style?: CSSProperties;
|
|
10
|
+
}
|
|
11
|
+
/** The property panel for an editor's selected nodes, or its document. */
|
|
12
|
+
export declare function ScriviamoInspector(props: ScriviamoInspectorProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// src/inspector.tsx
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
|
|
4
|
+
// src/inspectorbinding.ts
|
|
5
|
+
import { Editor } from "scriviamo";
|
|
6
|
+
var InspectorBinding = class {
|
|
7
|
+
inspector;
|
|
8
|
+
constructor(element, editor, collapsedSections = []) {
|
|
9
|
+
let Inspector = Editor.addon("inspector")?.Inspector;
|
|
10
|
+
if (!Inspector) throw new Error(`The inspector needs Scriviamo's inspector add-on: import it before showing the inspector, such as import "@scriviamo-pro/pro/addons/inspector".`);
|
|
11
|
+
this.inspector = new Inspector(element, { editor, collapsedSections });
|
|
12
|
+
}
|
|
13
|
+
update(editor) {
|
|
14
|
+
if (this.inspector.inspectedEditor !== editor) this.inspector.inspectedEditor = editor;
|
|
15
|
+
}
|
|
16
|
+
destroy() {
|
|
17
|
+
this.inspector.destroy();
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/inspector.tsx
|
|
22
|
+
import { jsx } from "react/jsx-runtime";
|
|
23
|
+
function ScriviamoInspector(props) {
|
|
24
|
+
const host = useRef(null);
|
|
25
|
+
const binding = useRef(null);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
let created = new InspectorBinding(host.current, props.editor, props.collapsedSections);
|
|
28
|
+
binding.current = created;
|
|
29
|
+
return () => {
|
|
30
|
+
created.destroy();
|
|
31
|
+
binding.current = null;
|
|
32
|
+
};
|
|
33
|
+
}, []);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
binding.current?.update(props.editor);
|
|
36
|
+
}, [props.editor]);
|
|
37
|
+
return /* @__PURE__ */ jsx("div", { ref: host, className: props.className, style: props.style });
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
ScriviamoInspector
|
|
41
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Editor } from "scriviamo";
|
|
2
|
+
/** What the binding uses of the inspector add-on's Inspector. */
|
|
3
|
+
export interface InspectorLike {
|
|
4
|
+
inspectedEditor: Editor | null;
|
|
5
|
+
destroy(): void;
|
|
6
|
+
}
|
|
7
|
+
/** Keeps an inspector following an editor. */
|
|
8
|
+
export declare class InspectorBinding {
|
|
9
|
+
readonly inspector: InspectorLike;
|
|
10
|
+
constructor(element: HTMLElement, editor: Editor | null, collapsedSections?: Array<string>);
|
|
11
|
+
update(editor: Editor | null): void;
|
|
12
|
+
destroy(): void;
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "scriviamo-react",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1-beta.16",
|
|
4
|
+
"description": "React components for the Scriviamo editor",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./inspector": {
|
|
15
|
+
"types": "./dist/inspector.d.ts",
|
|
16
|
+
"import": "./dist/inspector.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"scripts": {
|
|
24
|
+
"sync": "node ../sync-shared.mjs",
|
|
25
|
+
"check": "npm run sync && tsc -p . --noEmit",
|
|
26
|
+
"build": "npm run sync && tsc -p . && esbuild src/index.tsx src/inspector.tsx --bundle --format=esm --target=es2022 --jsx=automatic --external:react --external:react-dom --external:scriviamo --outdir=dist"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"react": ">=18",
|
|
30
|
+
"scriviamo": "^1.0.1-beta.16"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/react": "^19.1.0",
|
|
34
|
+
"@types/react-dom": "^19.1.0",
|
|
35
|
+
"esbuild": "^0.25.0",
|
|
36
|
+
"react": "^19.1.0",
|
|
37
|
+
"react-dom": "^19.1.0",
|
|
38
|
+
"typescript": "^5.8.0"
|
|
39
|
+
},
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"homepage": "https://scriviamo.dev",
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/Scriviamo-Pro/scriviamo.git",
|
|
45
|
+
"directory": "wrappers/react"
|
|
46
|
+
}
|
|
47
|
+
}
|