scriviamo-element 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 +109 -2
- package/dist/editorbinding.d.ts +67 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +360 -0
- package/dist/inspector.d.ts +24 -0
- package/dist/inspector.js +85 -0
- package/dist/inspectorbinding.d.ts +13 -0
- package/package.json +40 -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,110 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Scriviamo web component
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`<scriviamo-editor>` and `<scriviamo-inspector>`: the Scriviamo editor, and its inspector, as HTML
|
|
4
|
+
elements, for pages and apps with any framework, such as Svelte, Solid, Lit or Astro, or with none.
|
|
5
|
+
|
|
6
|
+
```html
|
|
7
|
+
<script type="module">
|
|
8
|
+
import "scriviamo"
|
|
9
|
+
import "scriviamo/dist/addons/scriviamo.callouts.js"
|
|
10
|
+
import "scriviamo-element"
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<form method="post" action="/save">
|
|
14
|
+
<scriviamo-editor name="body" format="markdown" status-bar style="height: 500px">
|
|
15
|
+
# Notes
|
|
16
|
+
|
|
17
|
+
Start writing.
|
|
18
|
+
</scriviamo-editor>
|
|
19
|
+
<button>Save</button>
|
|
20
|
+
</form>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Load Bootstrap 5's CSS and Bootstrap Icons, which Scriviamo's interface uses, then Scriviamo's
|
|
24
|
+
stylesheet, `dist/scriviamo.css`, and those of any add-ons, as you load other CSS. Bootstrap's
|
|
25
|
+
JavaScript isn't needed.
|
|
26
|
+
|
|
27
|
+
Importing `scriviamo-element` defines the element. Import the add-ons you use before it, so their
|
|
28
|
+
node types are there when the first editor is made. To use another tag name, import
|
|
29
|
+
`defineScriviamoEditor` and call it with the name before anything else defines the element.
|
|
30
|
+
|
|
31
|
+
## The starting document
|
|
32
|
+
|
|
33
|
+
The text inside the element is the document it starts with, in its `format`: Markdown, or HTML
|
|
34
|
+
with `format="html"`. The indent every line shares is removed, so it can sit indented in the
|
|
35
|
+
page. A `value` attribute takes its place, and for JSON, the default format, set the `value`
|
|
36
|
+
property from a script instead.
|
|
37
|
+
|
|
38
|
+
## Attributes
|
|
39
|
+
|
|
40
|
+
Each takes the setting of the same name in the [wrappers README](../README.md#settings):
|
|
41
|
+
|
|
42
|
+
| Attribute | Setting |
|
|
43
|
+
|---|---|
|
|
44
|
+
| `format` | `json`, `html` or `markdown` |
|
|
45
|
+
| `value` | The document, in `format` |
|
|
46
|
+
| `read-only`, `toolbar-visible`, `show-contextual-toolbars`, `hover-insertion`, `markdown-shortcuts`, `allow-annotations`, `adaptive-height`, `status-bar` | On when present; off when set to `"false"` |
|
|
47
|
+
| `toolbar-display-mode`, `font-size`, `change-delay` | Numbers |
|
|
48
|
+
| `font-name`, `editor-class` | Text |
|
|
49
|
+
| `name` | The name it's submitted under in a form |
|
|
50
|
+
|
|
51
|
+
Each also has a property, such as `element.readOnly = true`, which sets the attribute.
|
|
52
|
+
|
|
53
|
+
## Properties
|
|
54
|
+
|
|
55
|
+
| Property | Meaning |
|
|
56
|
+
|---|---|
|
|
57
|
+
| `value` | The document, in `format`. Setting it replaces the document; reading it gives the editor's current document. |
|
|
58
|
+
| `editor` | The `Editor`, for everything else, or null until the element is on the page |
|
|
59
|
+
| `autosave`, `fileUploader`, `locale`, `events` | The settings that can't be attributes |
|
|
60
|
+
| `settings` | All the settings set as properties, as one object |
|
|
61
|
+
|
|
62
|
+
## Events
|
|
63
|
+
|
|
64
|
+
| Event | `detail` |
|
|
65
|
+
|---|---|
|
|
66
|
+
| `input`, `change` | `{value, editor}`, as the document changes, after `change-delay` |
|
|
67
|
+
| `selectionchange` | `{node, editor}`, when the primary selected node changes |
|
|
68
|
+
| `ready` | `{editor}`, once the editor is made |
|
|
69
|
+
|
|
70
|
+
## In forms
|
|
71
|
+
|
|
72
|
+
The element is a form field: a form submits its document under its `name`, `FormData` includes
|
|
73
|
+
it, `form.reset()` brings back the document it started with, and a disabled `fieldset` makes it
|
|
74
|
+
read-only.
|
|
75
|
+
|
|
76
|
+
## Lifetime
|
|
77
|
+
|
|
78
|
+
The editor is made when the element joins the page and removed when it leaves. Moving the
|
|
79
|
+
element elsewhere on the page keeps its editor.
|
|
80
|
+
|
|
81
|
+
## The inspector
|
|
82
|
+
|
|
83
|
+
Imported from `scriviamo-element/inspector`. The inspector element needs Scriviamo's `inspector`
|
|
84
|
+
add-on, which is premium, so it has its own entry point. Import the add-on before showing the
|
|
85
|
+
inspector: `scriviamo/addons/inspector` from this repository, or
|
|
86
|
+
`@scriviamo-pro/pro/addons/inspector` (or `pro-collab`) from npm, with its stylesheet. The element
|
|
87
|
+
finds it whichever package it came from (see [The inspector](../../docs/inspector.md)); the editor
|
|
88
|
+
element works without it.
|
|
89
|
+
|
|
90
|
+
```html
|
|
91
|
+
<scriviamo-editor id="notes" format="markdown"></scriviamo-editor>
|
|
92
|
+
<scriviamo-inspector for="notes" collapsed-sections="appearance"></scriviamo-inspector>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
| Attribute or property | Meaning |
|
|
96
|
+
|---|---|
|
|
97
|
+
| `for` | The id of the `<scriviamo-editor>` to follow |
|
|
98
|
+
| `editor` | The `Editor` to follow, instead of `for` |
|
|
99
|
+
| `collapsed-sections` | Identifiers of sections that start collapsed, separated by spaces |
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
## Building
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
npm install
|
|
106
|
+
npm run build
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The build writes `dist/index.js` and its declarations. Scriviamo stays external, as a peer
|
|
110
|
+
dependency.
|
|
@@ -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,55 @@
|
|
|
1
|
+
import type { Editor } from "scriviamo";
|
|
2
|
+
import { type ScriviamoFormat, type ScriviamoSettings } from "./editorbinding";
|
|
3
|
+
export type { ScriviamoFormat, ScriviamoSettings } from "./editorbinding";
|
|
4
|
+
/**
|
|
5
|
+
* A Scriviamo editor as an HTML element, for pages and apps with any framework or none. Import the
|
|
6
|
+
* add-ons you use, and this module, which defines the element:
|
|
7
|
+
*
|
|
8
|
+
* <scriviamo-editor name="body" format="markdown" status-bar style="height: 500px">
|
|
9
|
+
* # Notes
|
|
10
|
+
*
|
|
11
|
+
* Start writing.
|
|
12
|
+
* </scriviamo-editor>
|
|
13
|
+
*
|
|
14
|
+
* The text inside the element is the document it starts with, in its format, unless it has a
|
|
15
|
+
* value attribute. Boolean attributes are on when present, and off when set to "false".
|
|
16
|
+
*
|
|
17
|
+
* Properties: value, format, editor (the Editor), a property for each attribute, such as readOnly,
|
|
18
|
+
* and the settings that can't be attributes: autosave, fileUploader, locale and events.
|
|
19
|
+
*
|
|
20
|
+
* Events: change and input (detail: {value, editor}) as the document changes; selectionchange
|
|
21
|
+
* (detail: {node, editor}); ready (detail: {editor}) once the editor is made.
|
|
22
|
+
*
|
|
23
|
+
* In a form, it's a form field: its value is submitted under its name, and form.reset() brings
|
|
24
|
+
* back the document it started with.
|
|
25
|
+
*/
|
|
26
|
+
export declare class ScriviamoEditorElement extends HTMLElement {
|
|
27
|
+
#private;
|
|
28
|
+
static formAssociated: boolean;
|
|
29
|
+
static get observedAttributes(): Array<string>;
|
|
30
|
+
constructor();
|
|
31
|
+
/** The Editor, once the element is on the page. */
|
|
32
|
+
get editor(): Editor | null;
|
|
33
|
+
/** The document, in format. Setting it replaces the document. */
|
|
34
|
+
get value(): string;
|
|
35
|
+
set value(value: string);
|
|
36
|
+
get format(): ScriviamoFormat;
|
|
37
|
+
set format(value: ScriviamoFormat);
|
|
38
|
+
get form(): HTMLFormElement | null;
|
|
39
|
+
get name(): string;
|
|
40
|
+
/** Settings with no attribute, or set from script: autosave, fileUploader, locale, events and the rest. */
|
|
41
|
+
get settings(): ScriviamoSettings;
|
|
42
|
+
set settings(value: ScriviamoSettings);
|
|
43
|
+
connectedCallback(): void;
|
|
44
|
+
disconnectedCallback(): void;
|
|
45
|
+
attributeChangedCallback(name: string, previous: string | null, next: string | null): void;
|
|
46
|
+
formResetCallback(): void;
|
|
47
|
+
formDisabledCallback(disabled: boolean): void;
|
|
48
|
+
}
|
|
49
|
+
/** Defines the element, as <scriviamo-editor> unless another name is given. Importing this module defines it. */
|
|
50
|
+
export declare function defineScriviamoEditor(name?: string): void;
|
|
51
|
+
declare global {
|
|
52
|
+
interface HTMLElementTagNameMap {
|
|
53
|
+
"scriviamo-editor": ScriviamoEditorElement;
|
|
54
|
+
}
|
|
55
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
// src/editorbinding.ts
|
|
2
|
+
import { Editor } from "scriviamo";
|
|
3
|
+
var EditorBinding = class _EditorBinding {
|
|
4
|
+
editor;
|
|
5
|
+
#settings;
|
|
6
|
+
#callbacks;
|
|
7
|
+
/** The value last reported, and the value last loaded from outside, so a wrapper passing either back doesn't reload the document. */
|
|
8
|
+
#value;
|
|
9
|
+
#loadedValue;
|
|
10
|
+
/** True while a value from outside is loading, whose changes aren't the user's. */
|
|
11
|
+
#loading = false;
|
|
12
|
+
#timer = null;
|
|
13
|
+
#listeners = [];
|
|
14
|
+
#onContent;
|
|
15
|
+
#onSelection;
|
|
16
|
+
constructor(element, settings, callbacks = {}) {
|
|
17
|
+
this.#settings = { ...settings };
|
|
18
|
+
this.#callbacks = callbacks;
|
|
19
|
+
this.editor = new Editor(element, _EditorBinding.#options(settings));
|
|
20
|
+
if (settings.value !== void 0) this.#load(settings.value, this.#format(settings));
|
|
21
|
+
this.#onContent = () => {
|
|
22
|
+
if (!this.#loading) this.#scheduleChange();
|
|
23
|
+
};
|
|
24
|
+
this.#onSelection = (event) => this.#callbacks.selectionChange?.(event.detail?.node ?? null, this.editor);
|
|
25
|
+
this.editor.addEventListener("contentChanged", this.#onContent);
|
|
26
|
+
this.editor.addEventListener("selectionChanged", this.#onSelection);
|
|
27
|
+
this.#listen(settings.events);
|
|
28
|
+
callbacks.ready?.(this.editor);
|
|
29
|
+
}
|
|
30
|
+
/** The options a new editor starts with. Content is loaded separately, in the chosen format. */
|
|
31
|
+
static #options(settings) {
|
|
32
|
+
let options = {};
|
|
33
|
+
if (settings.readOnly !== void 0) options.readOnly = settings.readOnly;
|
|
34
|
+
if (settings.toolbarVisible !== void 0) options.toolbarVisible = settings.toolbarVisible;
|
|
35
|
+
if (settings.toolbarDisplayMode !== void 0) options.toolbarDisplayMode = settings.toolbarDisplayMode;
|
|
36
|
+
if (settings.showContextualToolbars !== void 0) options.showContextualToolbars = settings.showContextualToolbars;
|
|
37
|
+
if (settings.hoverInsertion !== void 0) options.isHoverInsertionEnabled = settings.hoverInsertion;
|
|
38
|
+
if (settings.markdownShortcuts !== void 0) options.markdownShortcuts = settings.markdownShortcuts;
|
|
39
|
+
if (settings.allowAnnotations !== void 0) options.allowAnnotations = settings.allowAnnotations;
|
|
40
|
+
if (settings.adaptiveHeight !== void 0) options.adaptiveHeight = settings.adaptiveHeight;
|
|
41
|
+
if (settings.fontName) options.fontName = settings.fontName;
|
|
42
|
+
if (settings.fontSize) options.fontSize = settings.fontSize;
|
|
43
|
+
if (settings.locale) options.strings = settings.locale;
|
|
44
|
+
if (settings.editorClass) options.class = settings.editorClass;
|
|
45
|
+
if (settings.statusBar !== void 0) options.statusBar = settings.statusBar;
|
|
46
|
+
if (settings.autosave) options.autosave = settings.autosave;
|
|
47
|
+
if (settings.fileUploader) options.fileUploader = settings.fileUploader;
|
|
48
|
+
return options;
|
|
49
|
+
}
|
|
50
|
+
#format(settings) {
|
|
51
|
+
return settings.format == "html" || settings.format == "markdown" ? settings.format : "json";
|
|
52
|
+
}
|
|
53
|
+
/** Applies the settings that changed since previous. A new editor gets them as options instead. */
|
|
54
|
+
#apply(settings, previous) {
|
|
55
|
+
let editor = this.editor;
|
|
56
|
+
let changed = (name) => settings[name] !== void 0 && settings[name] !== previous[name];
|
|
57
|
+
if (changed("readOnly")) editor.readOnly = !!settings.readOnly;
|
|
58
|
+
if (changed("toolbarVisible")) editor.toolbarVisible = !!settings.toolbarVisible;
|
|
59
|
+
if (changed("toolbarDisplayMode")) editor.toolbarDisplayMode = settings.toolbarDisplayMode;
|
|
60
|
+
if (changed("showContextualToolbars")) editor.showContextualToolbars = !!settings.showContextualToolbars;
|
|
61
|
+
if (changed("hoverInsertion")) editor.isHoverInsertionEnabled = !!settings.hoverInsertion;
|
|
62
|
+
if (changed("markdownShortcuts")) editor.markdownShortcuts = !!settings.markdownShortcuts;
|
|
63
|
+
if (changed("allowAnnotations")) editor.allowAnnotations = !!settings.allowAnnotations;
|
|
64
|
+
if (changed("adaptiveHeight")) editor.adaptiveHeight = !!settings.adaptiveHeight;
|
|
65
|
+
if (changed("fontName")) editor.fontName = settings.fontName;
|
|
66
|
+
if (changed("fontSize")) editor.fontSize = settings.fontSize;
|
|
67
|
+
if (changed("editorClass")) editor.class = settings.editorClass;
|
|
68
|
+
if (changed("locale")) editor.localize(settings.locale);
|
|
69
|
+
if (changed("statusBar")) editor.statusBarVisible = !!settings.statusBar;
|
|
70
|
+
if (settings.autosave !== previous.autosave) editor.updateAutosave(settings.autosave ?? null);
|
|
71
|
+
if (settings.fileUploader !== previous.fileUploader) editor.fileUploader = settings.fileUploader ?? null;
|
|
72
|
+
}
|
|
73
|
+
/** Applies new settings from the framework component. */
|
|
74
|
+
update(settings) {
|
|
75
|
+
let previous = this.#settings;
|
|
76
|
+
this.#settings = { ...settings };
|
|
77
|
+
this.#apply(settings, previous);
|
|
78
|
+
if (settings.events !== previous.events) {
|
|
79
|
+
this.#unlisten();
|
|
80
|
+
this.#listen(settings.events);
|
|
81
|
+
}
|
|
82
|
+
let format = this.#format(settings);
|
|
83
|
+
let formatChanged = format != this.#format(previous);
|
|
84
|
+
let known = settings.value === this.#value || settings.value === this.#loadedValue;
|
|
85
|
+
if (settings.value !== void 0 && !known) {
|
|
86
|
+
this.#flush();
|
|
87
|
+
this.#load(settings.value, format);
|
|
88
|
+
} else if (formatChanged) {
|
|
89
|
+
this.#value = this.value;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** The document in the binding's format. */
|
|
93
|
+
get value() {
|
|
94
|
+
switch (this.#format(this.#settings)) {
|
|
95
|
+
case "html":
|
|
96
|
+
return this.editor.toHTML();
|
|
97
|
+
case "markdown":
|
|
98
|
+
return this.editor.toMarkdown();
|
|
99
|
+
}
|
|
100
|
+
return this.editor.toJSON();
|
|
101
|
+
}
|
|
102
|
+
#load(value, format) {
|
|
103
|
+
let editor = this.editor;
|
|
104
|
+
this.#loading = true;
|
|
105
|
+
try {
|
|
106
|
+
this.#loadInto(editor, value, format);
|
|
107
|
+
} finally {
|
|
108
|
+
this.#loading = false;
|
|
109
|
+
}
|
|
110
|
+
editor.history.reset();
|
|
111
|
+
editor.isChanged = false;
|
|
112
|
+
this.#loadedValue = value;
|
|
113
|
+
this.#value = this.value;
|
|
114
|
+
}
|
|
115
|
+
#loadInto(editor, value, format) {
|
|
116
|
+
if (format == "json") {
|
|
117
|
+
if (value.trim().length == 0) editor.removeAllNodes();
|
|
118
|
+
else editor.fromJSON(value);
|
|
119
|
+
} else if (format == "html") {
|
|
120
|
+
if (value.trim().length == 0) editor.removeAllNodes();
|
|
121
|
+
else editor.fromHTML(value);
|
|
122
|
+
} else {
|
|
123
|
+
if (value.trim().length == 0) editor.removeAllNodes();
|
|
124
|
+
else editor.fromMarkdown(value);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
#scheduleChange() {
|
|
128
|
+
let delay = Math.max(0, this.#settings.changeDelay ?? 0);
|
|
129
|
+
if (this.#timer) clearTimeout(this.#timer);
|
|
130
|
+
this.#timer = null;
|
|
131
|
+
if (delay == 0) {
|
|
132
|
+
this.#report();
|
|
133
|
+
} else {
|
|
134
|
+
this.#timer = setTimeout(() => {
|
|
135
|
+
this.#timer = null;
|
|
136
|
+
this.#report();
|
|
137
|
+
}, delay);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
#report() {
|
|
141
|
+
let value = this.value;
|
|
142
|
+
if (value === this.#value) return;
|
|
143
|
+
this.#value = value;
|
|
144
|
+
this.#loadedValue = void 0;
|
|
145
|
+
this.#callbacks.change?.(value, this.editor);
|
|
146
|
+
}
|
|
147
|
+
/** Reports a change that is waiting for changeDelay. */
|
|
148
|
+
#flush() {
|
|
149
|
+
if (!this.#timer) return;
|
|
150
|
+
clearTimeout(this.#timer);
|
|
151
|
+
this.#timer = null;
|
|
152
|
+
this.#report();
|
|
153
|
+
}
|
|
154
|
+
#listen(events) {
|
|
155
|
+
for (let name of Object.keys(events || {})) {
|
|
156
|
+
let handler = events[name];
|
|
157
|
+
if (typeof handler != "function") continue;
|
|
158
|
+
let listener = (event) => handler(event);
|
|
159
|
+
this.editor.addEventListener(name, listener);
|
|
160
|
+
this.#listeners.push([name, listener]);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
#unlisten() {
|
|
164
|
+
for (let [name, listener] of this.#listeners) this.editor.removeEventListener(name, listener);
|
|
165
|
+
this.#listeners = [];
|
|
166
|
+
}
|
|
167
|
+
/** Reports any waiting change, then removes the editor. */
|
|
168
|
+
destroy() {
|
|
169
|
+
this.#flush();
|
|
170
|
+
this.#unlisten();
|
|
171
|
+
this.editor.removeEventListener("contentChanged", this.#onContent);
|
|
172
|
+
this.editor.removeEventListener("selectionChanged", this.#onSelection);
|
|
173
|
+
this.editor.destroy();
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// src/index.ts
|
|
178
|
+
var Attributes = [
|
|
179
|
+
["format", "format", "string"],
|
|
180
|
+
["read-only", "readOnly", "boolean"],
|
|
181
|
+
["toolbar-visible", "toolbarVisible", "boolean"],
|
|
182
|
+
["toolbar-display-mode", "toolbarDisplayMode", "number"],
|
|
183
|
+
["show-contextual-toolbars", "showContextualToolbars", "boolean"],
|
|
184
|
+
["hover-insertion", "hoverInsertion", "boolean"],
|
|
185
|
+
["markdown-shortcuts", "markdownShortcuts", "boolean"],
|
|
186
|
+
["allow-annotations", "allowAnnotations", "boolean"],
|
|
187
|
+
["adaptive-height", "adaptiveHeight", "boolean"],
|
|
188
|
+
["font-name", "fontName", "string"],
|
|
189
|
+
["font-size", "fontSize", "number"],
|
|
190
|
+
["editor-class", "editorClass", "string"],
|
|
191
|
+
["status-bar", "statusBar", "boolean"],
|
|
192
|
+
["change-delay", "changeDelay", "number"]
|
|
193
|
+
];
|
|
194
|
+
var ScriviamoEditorElement = class extends HTMLElement {
|
|
195
|
+
static formAssociated = true;
|
|
196
|
+
static get observedAttributes() {
|
|
197
|
+
return ["value", ...Attributes.map(([attribute]) => attribute)];
|
|
198
|
+
}
|
|
199
|
+
#binding = null;
|
|
200
|
+
#host = null;
|
|
201
|
+
#internals = null;
|
|
202
|
+
/** Settings given as properties, which attributes don't cover. */
|
|
203
|
+
#properties = {};
|
|
204
|
+
#value = void 0;
|
|
205
|
+
#initialValue = void 0;
|
|
206
|
+
constructor() {
|
|
207
|
+
super();
|
|
208
|
+
if (typeof this.attachInternals == "function") {
|
|
209
|
+
try {
|
|
210
|
+
this.#internals = this.attachInternals();
|
|
211
|
+
} catch {
|
|
212
|
+
this.#internals = null;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/** The Editor, once the element is on the page. */
|
|
217
|
+
get editor() {
|
|
218
|
+
return this.#binding ? this.#binding.editor : null;
|
|
219
|
+
}
|
|
220
|
+
/** The document, in format. Setting it replaces the document. */
|
|
221
|
+
get value() {
|
|
222
|
+
return this.#binding ? this.#binding.value : this.#value ?? "";
|
|
223
|
+
}
|
|
224
|
+
set value(value) {
|
|
225
|
+
this.#value = String(value ?? "");
|
|
226
|
+
this.#update();
|
|
227
|
+
}
|
|
228
|
+
get format() {
|
|
229
|
+
let format = this.getAttribute("format");
|
|
230
|
+
return format == "html" || format == "markdown" ? format : "json";
|
|
231
|
+
}
|
|
232
|
+
set format(value) {
|
|
233
|
+
this.setAttribute("format", value);
|
|
234
|
+
}
|
|
235
|
+
get form() {
|
|
236
|
+
return this.#internals ? this.#internals.form : null;
|
|
237
|
+
}
|
|
238
|
+
get name() {
|
|
239
|
+
return this.getAttribute("name") || "";
|
|
240
|
+
}
|
|
241
|
+
/** Settings with no attribute, or set from script: autosave, fileUploader, locale, events and the rest. */
|
|
242
|
+
get settings() {
|
|
243
|
+
return { ...this.#properties };
|
|
244
|
+
}
|
|
245
|
+
set settings(value) {
|
|
246
|
+
this.#properties = { ...value };
|
|
247
|
+
this.#update();
|
|
248
|
+
}
|
|
249
|
+
connectedCallback() {
|
|
250
|
+
if (this.#binding) return;
|
|
251
|
+
if (this.#value === void 0 && !this.hasAttribute("value")) {
|
|
252
|
+
let inline = this.format == "html" ? this.innerHTML.trim() : this.#dedent(this.textContent || "");
|
|
253
|
+
if (inline.length > 0) this.#value = inline;
|
|
254
|
+
}
|
|
255
|
+
if (this.#value === void 0 && this.hasAttribute("value")) this.#value = this.getAttribute("value") || "";
|
|
256
|
+
this.#initialValue = this.#value;
|
|
257
|
+
this.replaceChildren();
|
|
258
|
+
if (!this.style.display) this.style.display = "block";
|
|
259
|
+
this.#host = document.createElement("div");
|
|
260
|
+
this.#host.style.height = "100%";
|
|
261
|
+
this.append(this.#host);
|
|
262
|
+
this.#binding = new EditorBinding(this.#host, this.#settings(), {
|
|
263
|
+
change: (value, editor) => {
|
|
264
|
+
this.#value = value;
|
|
265
|
+
this.#setFormValue(value);
|
|
266
|
+
this.dispatchEvent(new CustomEvent("input", { bubbles: true, detail: { value, editor } }));
|
|
267
|
+
this.dispatchEvent(new CustomEvent("change", { bubbles: true, detail: { value, editor } }));
|
|
268
|
+
},
|
|
269
|
+
selectionChange: (node, editor) => this.dispatchEvent(new CustomEvent("selectionchange", { detail: { node, editor } })),
|
|
270
|
+
ready: (editor) => this.dispatchEvent(new CustomEvent("ready", { detail: { editor } }))
|
|
271
|
+
});
|
|
272
|
+
this.#setFormValue(this.#binding.value);
|
|
273
|
+
}
|
|
274
|
+
disconnectedCallback() {
|
|
275
|
+
queueMicrotask(() => {
|
|
276
|
+
if (this.isConnected || !this.#binding) return;
|
|
277
|
+
this.#value = this.#binding.value;
|
|
278
|
+
this.#binding.destroy();
|
|
279
|
+
this.#binding = null;
|
|
280
|
+
this.#host = null;
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
attributeChangedCallback(name, previous, next) {
|
|
284
|
+
if (previous === next) return;
|
|
285
|
+
if (name == "value") this.#value = next ?? "";
|
|
286
|
+
this.#update();
|
|
287
|
+
}
|
|
288
|
+
formResetCallback() {
|
|
289
|
+
this.value = this.#initialValue ?? "";
|
|
290
|
+
}
|
|
291
|
+
formDisabledCallback(disabled) {
|
|
292
|
+
this.#properties = { ...this.#properties, readOnly: disabled ? true : this.#properties.readOnly };
|
|
293
|
+
this.#update();
|
|
294
|
+
}
|
|
295
|
+
/** Removes the indent every line of text shares, so the document can be indented in the page's HTML. */
|
|
296
|
+
#dedent(text) {
|
|
297
|
+
let lines = text.replace(/\r\n?/g, "\n").split("\n");
|
|
298
|
+
while (lines.length > 0 && lines[0].trim() == "") lines.shift();
|
|
299
|
+
while (lines.length > 0 && lines[lines.length - 1].trim() == "") lines.pop();
|
|
300
|
+
let indent = Math.min(...lines.filter((line) => line.trim().length > 0).map((line) => line.match(/^[ \t]*/)[0].length));
|
|
301
|
+
return lines.map((line) => line.substring(isFinite(indent) ? indent : 0)).join("\n");
|
|
302
|
+
}
|
|
303
|
+
#settings() {
|
|
304
|
+
let settings = { ...this.#properties };
|
|
305
|
+
for (let [attribute, setting, kind] of Attributes) {
|
|
306
|
+
if (!this.hasAttribute(attribute)) continue;
|
|
307
|
+
let text = this.getAttribute(attribute) || "";
|
|
308
|
+
if (kind == "boolean") settings[setting] = text != "false";
|
|
309
|
+
else if (kind == "number") settings[setting] = Number(text);
|
|
310
|
+
else settings[setting] = text;
|
|
311
|
+
}
|
|
312
|
+
if (this.#value !== void 0) settings.value = this.#value;
|
|
313
|
+
return settings;
|
|
314
|
+
}
|
|
315
|
+
#update() {
|
|
316
|
+
if (this.#binding) this.#binding.update(this.#settings());
|
|
317
|
+
}
|
|
318
|
+
#setFormValue(value) {
|
|
319
|
+
if (!this.#internals) return;
|
|
320
|
+
try {
|
|
321
|
+
this.#internals.setFormValue(value);
|
|
322
|
+
} catch {
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
for (let [attribute, setting, kind] of Attributes) {
|
|
327
|
+
if (setting == "format") continue;
|
|
328
|
+
Object.defineProperty(ScriviamoEditorElement.prototype, setting, {
|
|
329
|
+
configurable: true,
|
|
330
|
+
get() {
|
|
331
|
+
if (!this.hasAttribute(attribute)) return void 0;
|
|
332
|
+
let text = this.getAttribute(attribute) || "";
|
|
333
|
+
return kind == "boolean" ? text != "false" : kind == "number" ? Number(text) : text;
|
|
334
|
+
},
|
|
335
|
+
set(value) {
|
|
336
|
+
if (value === void 0 || value === null) this.removeAttribute(attribute);
|
|
337
|
+
else this.setAttribute(attribute, kind == "boolean" ? value ? "" : "false" : String(value));
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
for (let setting of ["autosave", "fileUploader", "locale", "events"]) {
|
|
342
|
+
Object.defineProperty(ScriviamoEditorElement.prototype, setting, {
|
|
343
|
+
configurable: true,
|
|
344
|
+
get() {
|
|
345
|
+
return this.settings[setting];
|
|
346
|
+
},
|
|
347
|
+
set(value) {
|
|
348
|
+
this.settings = { ...this.settings, [setting]: value };
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
function defineScriviamoEditor(name = "scriviamo-editor") {
|
|
353
|
+
if (!customElements.get(name)) customElements.define(name, name == "scriviamo-editor" ? ScriviamoEditorElement : class extends ScriviamoEditorElement {
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
defineScriviamoEditor();
|
|
357
|
+
export {
|
|
358
|
+
ScriviamoEditorElement,
|
|
359
|
+
defineScriviamoEditor
|
|
360
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Editor } from "scriviamo";
|
|
2
|
+
/**
|
|
3
|
+
* The property panel for an editor's selected nodes, or its document. Its for attribute names the
|
|
4
|
+
* id of the <scriviamo-editor> it follows; or set its editor property to an Editor.
|
|
5
|
+
*
|
|
6
|
+
* <scriviamo-editor id="notes"></scriviamo-editor>
|
|
7
|
+
* <scriviamo-inspector for="notes"></scriviamo-inspector>
|
|
8
|
+
*/
|
|
9
|
+
export declare class ScriviamoInspectorElement extends HTMLElement {
|
|
10
|
+
#private;
|
|
11
|
+
static get observedAttributes(): Array<string>;
|
|
12
|
+
get editor(): Editor | null;
|
|
13
|
+
set editor(value: Editor | null);
|
|
14
|
+
connectedCallback(): void;
|
|
15
|
+
disconnectedCallback(): void;
|
|
16
|
+
attributeChangedCallback(): void;
|
|
17
|
+
}
|
|
18
|
+
/** Defines the element, as <scriviamo-inspector> unless another name is given. Importing this module defines it. */
|
|
19
|
+
export declare function defineScriviamoInspector(name?: string): void;
|
|
20
|
+
declare global {
|
|
21
|
+
interface HTMLElementTagNameMap {
|
|
22
|
+
"scriviamo-inspector": ScriviamoInspectorElement;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// src/inspectorbinding.ts
|
|
2
|
+
import { Editor } from "scriviamo";
|
|
3
|
+
var InspectorBinding = class {
|
|
4
|
+
inspector;
|
|
5
|
+
constructor(element, editor, collapsedSections = []) {
|
|
6
|
+
let Inspector = Editor.addon("inspector")?.Inspector;
|
|
7
|
+
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".`);
|
|
8
|
+
this.inspector = new Inspector(element, { editor, collapsedSections });
|
|
9
|
+
}
|
|
10
|
+
update(editor) {
|
|
11
|
+
if (this.inspector.inspectedEditor !== editor) this.inspector.inspectedEditor = editor;
|
|
12
|
+
}
|
|
13
|
+
destroy() {
|
|
14
|
+
this.inspector.destroy();
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/inspector.ts
|
|
19
|
+
var ScriviamoInspectorElement = class extends HTMLElement {
|
|
20
|
+
static get observedAttributes() {
|
|
21
|
+
return ["for"];
|
|
22
|
+
}
|
|
23
|
+
#binding = null;
|
|
24
|
+
#editor = null;
|
|
25
|
+
#explicit = false;
|
|
26
|
+
#waiting = null;
|
|
27
|
+
#onReady = () => this.#follow();
|
|
28
|
+
get editor() {
|
|
29
|
+
return this.#binding ? this.#binding.inspector.inspectedEditor : this.#editor;
|
|
30
|
+
}
|
|
31
|
+
set editor(value) {
|
|
32
|
+
this.#explicit = value !== null;
|
|
33
|
+
this.#editor = value;
|
|
34
|
+
this.#follow();
|
|
35
|
+
}
|
|
36
|
+
connectedCallback() {
|
|
37
|
+
if (!this.style.display) this.style.display = "block";
|
|
38
|
+
if (!this.#binding) {
|
|
39
|
+
let host = document.createElement("div");
|
|
40
|
+
this.replaceChildren(host);
|
|
41
|
+
this.#binding = new InspectorBinding(host, null, (this.getAttribute("collapsed-sections") || "").split(/\s+/).filter(Boolean));
|
|
42
|
+
}
|
|
43
|
+
this.#follow();
|
|
44
|
+
}
|
|
45
|
+
disconnectedCallback() {
|
|
46
|
+
this.#stopWaiting();
|
|
47
|
+
queueMicrotask(() => {
|
|
48
|
+
if (this.isConnected || !this.#binding) return;
|
|
49
|
+
this.#binding.destroy();
|
|
50
|
+
this.#binding = null;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
attributeChangedCallback() {
|
|
54
|
+
this.#follow();
|
|
55
|
+
}
|
|
56
|
+
/** Follows the editor given, or the one the for attribute names, once it's ready. */
|
|
57
|
+
#follow() {
|
|
58
|
+
if (!this.#binding) return;
|
|
59
|
+
this.#stopWaiting();
|
|
60
|
+
let editor = this.#explicit ? this.#editor : null;
|
|
61
|
+
if (!this.#explicit) {
|
|
62
|
+
let id = this.getAttribute("for");
|
|
63
|
+
let target = id ? this.ownerDocument.getElementById(id) : null;
|
|
64
|
+
editor = target && target.editor ? target.editor : null;
|
|
65
|
+
if (target && !editor) {
|
|
66
|
+
this.#waiting = target;
|
|
67
|
+
target.addEventListener("ready", this.#onReady, { once: true });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
this.#binding.update(editor);
|
|
71
|
+
}
|
|
72
|
+
#stopWaiting() {
|
|
73
|
+
if (this.#waiting) this.#waiting.removeEventListener("ready", this.#onReady);
|
|
74
|
+
this.#waiting = null;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
function defineScriviamoInspector(name = "scriviamo-inspector") {
|
|
78
|
+
if (!customElements.get(name)) customElements.define(name, name == "scriviamo-inspector" ? ScriviamoInspectorElement : class extends ScriviamoInspectorElement {
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
defineScriviamoInspector();
|
|
82
|
+
export {
|
|
83
|
+
ScriviamoInspectorElement,
|
|
84
|
+
defineScriviamoInspector
|
|
85
|
+
};
|
|
@@ -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,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "scriviamo-element",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1-beta.16",
|
|
4
|
+
"description": "A web component for the Scriviamo editor: <scriviamo-editor>, for any framework or none",
|
|
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": true,
|
|
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.ts src/inspector.ts --bundle --format=esm --target=es2022 --external:scriviamo --outdir=dist"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"scriviamo": "^1.0.1-beta.16"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"esbuild": "^0.25.0",
|
|
33
|
+
"typescript": "^5.8.0"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"homepage": "https://scriviamo.dev",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/Scriviamo-Pro/scriviamo.git",
|
|
40
|
+
"directory": "wrappers/element"
|
|
41
|
+
}
|
|
42
|
+
}
|