svelte-multistyle-ui 0.0.9-alpha → 0.0.11-alpha

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.
@@ -0,0 +1,187 @@
1
+ <script>
2
+ import "./texteditor-styles.css";
3
+ import { defaults } from "../config.js";
4
+ import { cn } from "../utils/cn.js";
5
+ import { onMount, onDestroy } from "svelte";
6
+ import { TextEditorCore } from "./text-editor-core/index.js";
7
+ import { Modal, Button, Input, Textarea } from "../index.js";
8
+
9
+ let {
10
+ style: styleProp,
11
+ theme: themeProp,
12
+ value = $bindable(""),
13
+ label = "",
14
+ placeholder = "",
15
+ disabled = false,
16
+ rows = 8,
17
+ toolbar,
18
+ class: className = "",
19
+ ...rest
20
+ } = $props();
21
+
22
+ const style = $derived(styleProp ?? defaults.style);
23
+ const theme = $derived(themeProp ?? defaults.theme);
24
+
25
+ const styleClass = $derived(`s-texteditor-${style}`);
26
+ const themeClass = $derived(`theme-${theme}`);
27
+
28
+ let wrapper = $state(null);
29
+ let core = $state(null);
30
+ let previousDisabled = $state(disabled);
31
+
32
+ let dialog = $state({ open: false, type: null, data: null });
33
+ let imageFileInput = $state(null);
34
+
35
+ onMount(() => {
36
+ core = new TextEditorCore({
37
+ element: wrapper,
38
+ placeholder,
39
+ readOnly: disabled,
40
+ toolbar,
41
+ onChange: (html) => {
42
+ value = html;
43
+ },
44
+ onAction: (action) => {
45
+ if (action.type === "dialog") {
46
+ dialog = { open: true, type: action.dialog, data: { ...action.data } };
47
+ }
48
+ },
49
+ });
50
+
51
+ core.setHTML(value ?? "");
52
+ });
53
+
54
+ $effect(() => {
55
+ if (!core) return;
56
+ // Don't overwrite code-view textarea from external value changes.
57
+ if (core.isCodeView()) return;
58
+
59
+ const html = value ?? "";
60
+ if (core.getHTML() !== html) {
61
+ core.setHTML(html);
62
+ }
63
+ });
64
+
65
+ $effect(() => {
66
+ if (!core) return;
67
+ if (disabled !== previousDisabled) {
68
+ core.setReadOnly(disabled);
69
+ previousDisabled = disabled;
70
+ }
71
+ });
72
+
73
+ onDestroy(() => {
74
+ core?.destroy();
75
+ });
76
+
77
+ function applyLink() {
78
+ const { text, href, title, target } = dialog.data;
79
+ core?.insertLink({ text, href, title, target });
80
+ dialog = { open: false, type: null, data: null };
81
+ }
82
+
83
+ function applyImage() {
84
+ const { src, alt, width, align, caption } = dialog.data;
85
+ core?.insertImage({ src, alt, width, align, caption });
86
+ dialog = { open: false, type: null, data: null };
87
+ }
88
+
89
+ function applyVideo() {
90
+ const { src, width, height } = dialog.data;
91
+ core?.insertVideo({ src, width, height });
92
+ dialog = { open: false, type: null, data: null };
93
+ }
94
+
95
+ function applyTable() {
96
+ const { rows, cols } = dialog.data;
97
+ core?.insertTable({ rows, cols });
98
+ dialog = { open: false, type: null, data: null };
99
+ }
100
+
101
+ function onImageFileChange(e) {
102
+ const file = e.target.files?.[0];
103
+ if (!file) return;
104
+ const reader = new FileReader();
105
+ reader.onload = (ev) => {
106
+ dialog.data = { ...dialog.data, src: ev.target.result };
107
+ };
108
+ reader.readAsDataURL(file);
109
+ }
110
+ </script>
111
+
112
+ <div
113
+ class={cn("s-texteditor-wrapper", styleClass, themeClass, className)}
114
+ {...rest}
115
+ >
116
+ {#if label}
117
+ <label class="s-texteditor-label">{label}</label>
118
+ {/if}
119
+ <div
120
+ bind:this={wrapper}
121
+ class="s-texteditor-root"
122
+ data-placeholder={placeholder}
123
+ style:--texteditor-rows={rows}
124
+ aria-disabled={disabled}
125
+ ></div>
126
+ </div>
127
+
128
+ {#if dialog.open}
129
+ {#if dialog.type === "link"}
130
+ <Modal bind:open={dialog.open} title="Insert link" size="small">
131
+ <div class="s-texteditor-dialog">
132
+ <Input label="Text" bind:value={dialog.data.text} />
133
+ <Input label="URL" bind:value={dialog.data.href} />
134
+ <Input label="Title" bind:value={dialog.data.title} />
135
+ <Input label="Target" bind:value={dialog.data.target} />
136
+ <div class="s-texteditor-dialog-actions">
137
+ <Button onclick={applyLink}>Apply</Button>
138
+ <Button variant="outlined" onclick={() => (dialog.open = false)}>Cancel</Button>
139
+ </div>
140
+ </div>
141
+ </Modal>
142
+ {:else if dialog.type === "image"}
143
+ <Modal bind:open={dialog.open} title="Insert image" size="small">
144
+ <div class="s-texteditor-dialog">
145
+ <Input label="Image URL" bind:value={dialog.data.src} />
146
+ <div class="s-texteditor-dialog-row">
147
+ <input
148
+ bind:this={imageFileInput}
149
+ type="file"
150
+ accept="image/*"
151
+ onchange={onImageFileChange}
152
+ />
153
+ </div>
154
+ <Input label="Alt text" bind:value={dialog.data.alt} />
155
+ <Input label="Width (e.g. 200px)" bind:value={dialog.data.width} />
156
+ <Input label="Caption" bind:value={dialog.data.caption} />
157
+ <div class="s-texteditor-dialog-actions">
158
+ <Button onclick={applyImage}>Insert</Button>
159
+ <Button variant="outlined" onclick={() => (dialog.open = false)}>Cancel</Button>
160
+ </div>
161
+ </div>
162
+ </Modal>
163
+ {:else if dialog.type === "video"}
164
+ <Modal bind:open={dialog.open} title="Insert video" size="small">
165
+ <div class="s-texteditor-dialog">
166
+ <Textarea label="YouTube, Vimeo, or direct URL" rows={3} bind:value={dialog.data.src} />
167
+ <Input label="Width" bind:value={dialog.data.width} />
168
+ <Input label="Height" bind:value={dialog.data.height} />
169
+ <div class="s-texteditor-dialog-actions">
170
+ <Button onclick={applyVideo}>Insert</Button>
171
+ <Button variant="outlined" onclick={() => (dialog.open = false)}>Cancel</Button>
172
+ </div>
173
+ </div>
174
+ </Modal>
175
+ {:else if dialog.type === "table"}
176
+ <Modal bind:open={dialog.open} title="Insert table" size="small">
177
+ <div class="s-texteditor-dialog">
178
+ <Input label="Rows" type="number" bind:value={dialog.data.rows} />
179
+ <Input label="Columns" type="number" bind:value={dialog.data.cols} />
180
+ <div class="s-texteditor-dialog-actions">
181
+ <Button onclick={applyTable}>Insert</Button>
182
+ <Button variant="outlined" onclick={() => (dialog.open = false)}>Cancel</Button>
183
+ </div>
184
+ </div>
185
+ </Modal>
186
+ {/if}
187
+ {/if}
@@ -0,0 +1,27 @@
1
+ export default TextEditor;
2
+ type TextEditor = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ declare const TextEditor: import("svelte").Component<{
7
+ style: any;
8
+ theme: any;
9
+ value?: string;
10
+ label?: string;
11
+ placeholder?: string;
12
+ disabled?: boolean;
13
+ rows?: number;
14
+ toolbar: any;
15
+ class?: string;
16
+ } & Record<string, any>, {}, "value">;
17
+ type $$ComponentProps = {
18
+ style: any;
19
+ theme: any;
20
+ value?: string;
21
+ label?: string;
22
+ placeholder?: string;
23
+ disabled?: boolean;
24
+ rows?: number;
25
+ toolbar: any;
26
+ class?: string;
27
+ } & Record<string, any>;
@@ -30,7 +30,7 @@ export function buildData(type: any, { data, series, labels }: {
30
30
  labels: any;
31
31
  }, tokens: any): {
32
32
  datasets: {
33
- label: any;
33
+ label: undefined;
34
34
  data: any;
35
35
  backgroundColor: any;
36
36
  }[];
@@ -122,6 +122,6 @@ export function buildOptions(type: string, style: string, tokens: object, { titl
122
122
  padding: {
123
123
  bottom: number;
124
124
  };
125
- };
125
+ } | undefined;
126
126
  };
127
127
  };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Small DOM helpers used by the custom WYSIWYG editor core.
3
+ */
4
+ export function createElement(tag: any, attrs?: {}, children?: any[]): any;
5
+ export function closest(node: any, selector: any): any;
6
+ export function matches(el: any, selector: any): any;
7
+ export function isInsideEditor(node: any, editorEl: any): any;
8
+ export function getActiveElement(editorEl: any): Node | null;
9
+ export function getParentBlock(node: any, editorEl: any): any;
10
+ export function isBlockElement(el: any): boolean;
11
+ export function walkNodes(root: any, callback: any): void;
12
+ export function emptyNode(node: any): void;
13
+ export function ensureParagraph(editorEl: any): any;
14
+ export function stripTags(html: any, allowed: any): string;
15
+ export function getTextLength(html: any): number;
16
+ export function getWordCount(html: any): number;
17
+ export function htmlToText(html: any): string;
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Small DOM helpers used by the custom WYSIWYG editor core.
3
+ */
4
+
5
+ export function createElement(tag, attrs = {}, children = []) {
6
+ const el = document.createElement(tag);
7
+ for (const [key, value] of Object.entries(attrs)) {
8
+ if (key === "className") {
9
+ el.className = value;
10
+ } else if (key === "innerHTML") {
11
+ el.innerHTML = value;
12
+ } else if (key === "textContent") {
13
+ el.textContent = value;
14
+ } else if (key === "value") {
15
+ el.value = value;
16
+ } else if (key.startsWith("on") && typeof value === "function") {
17
+ const event = key.slice(2).toLowerCase();
18
+ el.addEventListener(event, value);
19
+ } else if (value !== null && value !== undefined) {
20
+ el.setAttribute(key, value);
21
+ }
22
+ }
23
+ for (const child of children) {
24
+ if (child == null) continue;
25
+ if (typeof child === "string" || typeof child === "number") {
26
+ el.appendChild(document.createTextNode(String(child)));
27
+ } else if (child instanceof Node) {
28
+ el.appendChild(child);
29
+ }
30
+ }
31
+ return el;
32
+ }
33
+
34
+ export function closest(node, selector) {
35
+ if (!node) return null;
36
+ let current = node;
37
+ while (current) {
38
+ if (current.nodeType === Node.ELEMENT_NODE && current.matches(selector)) {
39
+ return current;
40
+ }
41
+ current = current.parentNode;
42
+ }
43
+ return null;
44
+ }
45
+
46
+ export function matches(el, selector) {
47
+ if (!el || el.nodeType !== Node.ELEMENT_NODE) return false;
48
+ return el.matches?.(selector) ?? false;
49
+ }
50
+
51
+ export function isInsideEditor(node, editorEl) {
52
+ if (!node) return false;
53
+ if (node.nodeType === Node.TEXT_NODE) node = node.parentNode;
54
+ return editorEl.contains(node);
55
+ }
56
+
57
+ export function getActiveElement(editorEl) {
58
+ const sel = window.getSelection();
59
+ if (!sel || sel.rangeCount === 0) return null;
60
+ const node = sel.anchorNode;
61
+ if (!isInsideEditor(node, editorEl)) return null;
62
+ return node;
63
+ }
64
+
65
+ export function getParentBlock(node, editorEl) {
66
+ let current = node;
67
+ while (current && current !== editorEl) {
68
+ if (current.nodeType === Node.ELEMENT_NODE && isBlockElement(current)) {
69
+ return current;
70
+ }
71
+ current = current.parentNode;
72
+ }
73
+ return null;
74
+ }
75
+
76
+ export function isBlockElement(el) {
77
+ if (!el || el.nodeType !== Node.ELEMENT_NODE) return false;
78
+ const blocks = [
79
+ "P", "DIV", "H1", "H2", "H3", "H4", "H5", "H6",
80
+ "BLOCKQUOTE", "PRE", "UL", "OL", "LI", "TABLE",
81
+ "THEAD", "TBODY", "TR", "TD", "TH", "HR",
82
+ ];
83
+ return blocks.includes(el.tagName);
84
+ }
85
+
86
+ export function walkNodes(root, callback) {
87
+ const walker = document.createTreeWalker(
88
+ root,
89
+ NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
90
+ null,
91
+ false,
92
+ );
93
+ const nodes = [];
94
+ let node;
95
+ while ((node = walker.nextNode())) {
96
+ nodes.push(node);
97
+ }
98
+ for (const n of nodes) {
99
+ callback(n);
100
+ }
101
+ }
102
+
103
+ export function emptyNode(node) {
104
+ while (node.firstChild) {
105
+ node.removeChild(node.firstChild);
106
+ }
107
+ }
108
+
109
+ export function ensureParagraph(editorEl) {
110
+ if (!editorEl.innerHTML.trim()) {
111
+ editorEl.innerHTML = "<p><br></p>";
112
+ return editorEl.querySelector("p");
113
+ }
114
+ return null;
115
+ }
116
+
117
+ export function stripTags(html, allowed) {
118
+ const div = document.createElement("div");
119
+ div.innerHTML = html;
120
+ walkNodes(div, (node) => {
121
+ if (node.nodeType === Node.ELEMENT_NODE) {
122
+ const tag = node.tagName.toLowerCase();
123
+ if (!allowed.includes(tag)) {
124
+ const parent = node.parentNode;
125
+ while (node.firstChild) {
126
+ parent.insertBefore(node.firstChild, node);
127
+ }
128
+ parent.removeChild(node);
129
+ }
130
+ }
131
+ });
132
+ return div.innerHTML;
133
+ }
134
+
135
+ export function getTextLength(html) {
136
+ const div = document.createElement("div");
137
+ div.innerHTML = html;
138
+ return div.textContent.length;
139
+ }
140
+
141
+ export function getWordCount(html) {
142
+ const text = htmlToText(html);
143
+ const words = text.trim().split(/\s+/).filter(Boolean);
144
+ return words.length;
145
+ }
146
+
147
+ export function htmlToText(html) {
148
+ const div = document.createElement("div");
149
+ div.innerHTML = html;
150
+ return div.textContent || "";
151
+ }
@@ -0,0 +1,113 @@
1
+ export class TextEditorCore {
2
+ constructor(options?: {});
3
+ container: any;
4
+ onChange: any;
5
+ onSelectionChange: any;
6
+ onAction: any;
7
+ placeholder: any;
8
+ readOnly: any;
9
+ historyLimit: any;
10
+ toolbarConfig: any;
11
+ value: string;
12
+ history: {
13
+ record: (html: any) => void;
14
+ undo: (currentHtml: any) => any;
15
+ redo: () => any;
16
+ canUndo: () => boolean;
17
+ canRedo: () => boolean;
18
+ startComposition: () => void;
19
+ endComposition: (html: any) => void;
20
+ clear: () => void;
21
+ readonly length: number;
22
+ };
23
+ isComposing: boolean;
24
+ lastHtml: string;
25
+ focused: boolean;
26
+ _handlers: any[];
27
+ _dialogState: {
28
+ type: null;
29
+ data: null;
30
+ };
31
+ _codeView: boolean;
32
+ _codeEl: any;
33
+ _lastCodeValue: string;
34
+ _savedSelection: Range | null;
35
+ _buildDOM(): void;
36
+ toolbarEl: any;
37
+ bodyEl: any;
38
+ editorEl: any;
39
+ statusEl: any;
40
+ _attachEvents(): void;
41
+ _listen(target: any, event: any, handler: any): void;
42
+ destroy(): void;
43
+ _onInput(): void;
44
+ _onCompositionStart(): void;
45
+ _onCompositionEnd(): void;
46
+ _emitChange(): void;
47
+ _onFocus(): void;
48
+ _onBlur(): void;
49
+ _onSelectionChange(): void;
50
+ _onKeyDown(e: any): void;
51
+ _applyFormat(command: any): void;
52
+ _onPaste(e: any): void;
53
+ _onDrop(e: any): void;
54
+ _insertImageFromFile(file: any): void;
55
+ _renderToolbar(): void;
56
+ _createControl(action: any): any;
57
+ _handleCommand(action: any, value: any): void;
58
+ exec(command: any, value?: null): void;
59
+ _applyBlockFormat(tag: any): void;
60
+ _applyLineHeight(value: any): void;
61
+ _openLinkDialog(): void;
62
+ _openImageDialog(): void;
63
+ _openVideoDialog(): void;
64
+ _openTableDialog(): void;
65
+ _getSelectedLink(): Node | null;
66
+ insertLink({ text, href, title, target }: {
67
+ text: any;
68
+ href: any;
69
+ title: any;
70
+ target: any;
71
+ }): void;
72
+ removeLink(): void;
73
+ insertImage({ src, alt, width, align, caption }: {
74
+ src: any;
75
+ alt: any;
76
+ width: any;
77
+ align: any;
78
+ caption: any;
79
+ }): void;
80
+ insertVideo({ src, width, height }: {
81
+ src: any;
82
+ width?: string | undefined;
83
+ height?: string | undefined;
84
+ }): void;
85
+ insertTable({ rows, cols }: {
86
+ rows: any;
87
+ cols: any;
88
+ }): void;
89
+ getSelectedImage(): any;
90
+ alignImage(align: any): void;
91
+ removeImage(): void;
92
+ replaceImage(src: any): void;
93
+ setImageAlt(alt: any): void;
94
+ _recordBeforeMutation(): void;
95
+ undo(): void;
96
+ redo(): void;
97
+ toggleCodeView(): void;
98
+ _enterCodeView(): void;
99
+ _exitCodeView(): void;
100
+ _saveSelection(): Range | null;
101
+ _restoreSelection(range: any): void;
102
+ isCodeView(): boolean;
103
+ getHTML(): string;
104
+ setHTML(html: any): void;
105
+ _setHTMLInternal(html: any, record: any): void;
106
+ getText(): string;
107
+ clear(): void;
108
+ focus(): void;
109
+ setReadOnly(readOnly: any): void;
110
+ _updateToolbarState(state: any): void;
111
+ _updatePlaceholder(): void;
112
+ _updateStatus(state?: null): void;
113
+ }