@flexiui/svelte-rich-text 0.0.1

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 ADDED
@@ -0,0 +1,184 @@
1
+ A 'RichText' component for Svelte.
2
+ ## Installation
3
+
4
+ To install the `RichText` component in your Svelte project, you can use npm or yarn.
5
+
6
+ With npm:
7
+
8
+ ```bash
9
+ npm install @flexiui/svelte-rich-text
10
+ ```
11
+
12
+ With yarn:
13
+
14
+ ```bash
15
+ yarn add @flexiui/svelte-rich-text
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ Once installed, you can use the `RichText` component in your Svelte application.
21
+
22
+ ```svelte
23
+ <script lang="ts">
24
+ import { RichText } from '@flexiui/svelte-rich-text';
25
+ import { SpecialBox } from './editor/custom-extensions/SpecialBox';
26
+
27
+ let editor = $state(null);
28
+
29
+ let html = $state(null);
30
+ let json = $state(null);
31
+
32
+ let customExtensions = [
33
+ SpecialBox
34
+ ]
35
+
36
+ let content = `
37
+ <p>This is a simple and flexible "RichText" component designed to generate a WYSIWYG editor.</p>
38
+ <p>It allows easy customization of styles, icons, colors, and more. Additionally, it includes an editable mode for use in forms, block editors, etc.</p>
39
+ <p>This component is built on top of <a href="https://tiptap.dev/">Tiptap</a>, a powerful and flexible rich text editor framework for Svelte.</p>
40
+ `
41
+
42
+ // Editor events
43
+ function handleEditorBeforeCreate(e: any) {
44
+ console.log('beforeCreate');
45
+ console.log(e);
46
+ }
47
+
48
+ function handleEditorCreate(e: any) {
49
+ editor = e.editor;
50
+ html = editor.getHTML();
51
+ json = editor.getJSON();
52
+ }
53
+
54
+ function handleEditorDestroy(e: any) {
55
+ console.log('destroy');
56
+ console.log(e);
57
+ }
58
+
59
+ function handleEditorUpdate(e: any) {
60
+ console.log(e);
61
+ const { editor } = e;
62
+ html = editor.getHTML();
63
+ json = editor.getJSON();
64
+ }
65
+
66
+ function handleEditorTransaction(e: any) {
67
+ console.log('transaction');
68
+ console.log(e);
69
+ }
70
+
71
+ function handleEditorFocus(e: any) {
72
+ console.log('focus');
73
+ console.log(e);
74
+ }
75
+
76
+ function handleEditorBlur(e: any) {
77
+ console.log('blur');
78
+ console.log(e);
79
+ }
80
+
81
+ function handleEditorDrop(e: any) {
82
+ console.log('drop');
83
+ console.log(e);
84
+ }
85
+
86
+ function handleEditorDelete(e: any) {
87
+ console.log('delete');
88
+ console.log(e);
89
+ }
90
+
91
+ function handleEditorPaste(e: any) {
92
+ console.log('paste');
93
+ console.log(e);
94
+ }
95
+
96
+ function handleEditorSelectionUpdate(e: any) {
97
+ console.log('selectionUpdate');
98
+ console.log(e);
99
+ }
100
+
101
+ function handleEditorContentError(e: any) {
102
+ console.log('contentError');
103
+ console.log(e);
104
+ }
105
+ </script>
106
+
107
+ <RichText
108
+ className="my-rich-text"
109
+ id="flexi-rich-text"
110
+ customExtensions={customExtensions}
111
+ editorEvents={{
112
+ onUpdate: handleEditorUpdate,
113
+ onTransaction: handleEditorTransaction,
114
+ onFocus: handleEditorFocus,
115
+ onBlur: handleEditorBlur,
116
+ onDestroy: handleEditorDestroy,
117
+ onDrop: handleEditorDrop,
118
+ onDelete: handleEditorDelete,
119
+ onPaste: handleEditorPaste,
120
+ onSelectionUpdate: handleEditorSelectionUpdate,
121
+ onBeforeCreate: handleEditorBeforeCreate,
122
+ onCreate: handleEditorCreate,
123
+ onContentError: handleEditorContentError,
124
+ }}
125
+ >
126
+ </RichText>
127
+
128
+ <h2>JSON Output</h2>
129
+ <pre>{JSON.stringify(json, null, 2)}</pre>
130
+
131
+ <h2>HTML Output</h2>
132
+ <pre>{html}</pre>
133
+
134
+ ```
135
+
136
+ ## Props
137
+
138
+ | Prop name | Type | Default value | Description |
139
+ | --- | --- | --- | --- |
140
+ | className | string | '' | A string that sets the class attribute of the component.
141
+ | editable | boolean | false | A boolean that determines whether the editor is editable or not.
142
+ | content | any | null | The initial content of the editor.
143
+ | customExtensions | any[] | [] | An array of custom extensions to be added to the editor.
144
+ | editorEvents | object | {} | An object containing event handlers for various editor events.
145
+
146
+ ## Events
147
+
148
+ | Event name | Parameters | Description |
149
+ | --- | --- | --- |
150
+ | onTransaction | { editor, transaction } | Triggered when a transaction is executed on the editor.
151
+ | onBeforeCreate | { editor } | Triggered before the editor is created.
152
+ | onCreate | { editor } | Triggered after the editor is created.
153
+ | onUpdate | { editor, html, json } | Triggered when the editor content is updated.
154
+ | onFocus | { editor, event } | Triggered when the editor is focused.
155
+ | onBlur | { editor, event } | Triggered when the editor is blurred.
156
+ | onDestroy | { editor, message } | Triggered when the editor is destroyed.
157
+ | onDrop | { editor, event, slice, moved } | Triggered when the editor is dropped into the editor.
158
+ | onDelete | { editor, type, deletedRange, newRange, partial, from, to } | Triggered when content is deleted from the editor.
159
+ | onContentError | { editor, error, disableCollaboration } | Triggered when the editor content does not match the schema.
160
+ | onSelectionUpdate | { editor } | Triggered when the selection in the editor is updated.
161
+ | onPaste | { event, slice } | Triggered when the editor is pasted into.
162
+
163
+ ## Custom Extensions
164
+
165
+ You can add custom extensions to the editor by passing them as an array to the `customExtensions` prop. Here's an example of how to add the `SpecialBox` extension:
166
+
167
+ ```svelte
168
+ <script lang="ts">
169
+ import { SpecialBox } from './editor/custom-extensions/SpecialBox';
170
+
171
+ let customExtensions = [
172
+ SpecialBox
173
+ ]
174
+ </script>
175
+
176
+ <RichText
177
+ customExtensions={customExtensions}
178
+ ...
179
+ />
180
+ ```
181
+
182
+ In this example, the `SpecialBox` extension is imported from a file called `SpecialBox.ts` in the `editor/custom-extensions` directory.
183
+
184
+ More information about custom nodes, marks and extensions can be found in the [Tiptap documentation](https://tiptap.dev/docs/editor/extensions/custom-extensions/create-new).