@pie-lib/editable-html-tip-tap 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.
Files changed (167) hide show
  1. package/CHANGELOG.json +32 -0
  2. package/CHANGELOG.md +2280 -0
  3. package/lib/__tests__/editor.test.js +470 -0
  4. package/lib/__tests__/serialization.test.js +246 -0
  5. package/lib/__tests__/utils.js +106 -0
  6. package/lib/block-tags.js +25 -0
  7. package/lib/constants.js +16 -0
  8. package/lib/editor.js +1356 -0
  9. package/lib/extensions/MediaView.js +112 -0
  10. package/lib/extensions/characters.js +65 -0
  11. package/lib/extensions/component.js +325 -0
  12. package/lib/extensions/css.js +252 -0
  13. package/lib/extensions/custom-toolbar-wrapper.js +124 -0
  14. package/lib/extensions/image.js +106 -0
  15. package/lib/extensions/math.js +330 -0
  16. package/lib/extensions/media.js +276 -0
  17. package/lib/extensions/responseArea.js +278 -0
  18. package/lib/index.js +1213 -0
  19. package/lib/old-index.js +269 -0
  20. package/lib/parse-html.js +16 -0
  21. package/lib/plugins/characters/custom-popper.js +73 -0
  22. package/lib/plugins/characters/index.js +305 -0
  23. package/lib/plugins/characters/utils.js +381 -0
  24. package/lib/plugins/css/icons/index.js +37 -0
  25. package/lib/plugins/css/index.js +390 -0
  26. package/lib/plugins/customPlugin/index.js +114 -0
  27. package/lib/plugins/html/icons/index.js +38 -0
  28. package/lib/plugins/html/index.js +81 -0
  29. package/lib/plugins/image/__tests__/component.test.js +51 -0
  30. package/lib/plugins/image/__tests__/image-toolbar-logic.test.js +56 -0
  31. package/lib/plugins/image/__tests__/image-toolbar.test.js +26 -0
  32. package/lib/plugins/image/__tests__/index.test.js +98 -0
  33. package/lib/plugins/image/__tests__/insert-image-handler.test.js +125 -0
  34. package/lib/plugins/image/__tests__/mock-change.js +25 -0
  35. package/lib/plugins/image/alt-dialog.js +129 -0
  36. package/lib/plugins/image/component.js +419 -0
  37. package/lib/plugins/image/image-toolbar.js +177 -0
  38. package/lib/plugins/image/index.js +263 -0
  39. package/lib/plugins/image/insert-image-handler.js +117 -0
  40. package/lib/plugins/index.js +413 -0
  41. package/lib/plugins/list/__tests__/index.test.js +79 -0
  42. package/lib/plugins/list/index.js +334 -0
  43. package/lib/plugins/math/__tests__/index.test.js +300 -0
  44. package/lib/plugins/math/index.js +454 -0
  45. package/lib/plugins/media/__tests__/index.test.js +71 -0
  46. package/lib/plugins/media/index.js +387 -0
  47. package/lib/plugins/media/media-dialog.js +709 -0
  48. package/lib/plugins/media/media-toolbar.js +101 -0
  49. package/lib/plugins/media/media-wrapper.js +93 -0
  50. package/lib/plugins/rendering/index.js +46 -0
  51. package/lib/plugins/respArea/drag-in-the-blank/choice.js +289 -0
  52. package/lib/plugins/respArea/drag-in-the-blank/index.js +94 -0
  53. package/lib/plugins/respArea/explicit-constructed-response/index.js +120 -0
  54. package/lib/plugins/respArea/icons/index.js +95 -0
  55. package/lib/plugins/respArea/index.js +341 -0
  56. package/lib/plugins/respArea/inline-dropdown/index.js +126 -0
  57. package/lib/plugins/respArea/math-templated/index.js +130 -0
  58. package/lib/plugins/respArea/utils.js +125 -0
  59. package/lib/plugins/table/CustomTablePlugin.js +133 -0
  60. package/lib/plugins/table/__tests__/index.test.js +442 -0
  61. package/lib/plugins/table/__tests__/table-toolbar.test.js +54 -0
  62. package/lib/plugins/table/icons/index.js +69 -0
  63. package/lib/plugins/table/index.js +483 -0
  64. package/lib/plugins/table/table-toolbar.js +187 -0
  65. package/lib/plugins/textAlign/icons/index.js +194 -0
  66. package/lib/plugins/textAlign/index.js +34 -0
  67. package/lib/plugins/toolbar/__tests__/default-toolbar.test.js +128 -0
  68. package/lib/plugins/toolbar/__tests__/editor-and-toolbar.test.js +51 -0
  69. package/lib/plugins/toolbar/__tests__/toolbar-buttons.test.js +54 -0
  70. package/lib/plugins/toolbar/__tests__/toolbar.test.js +120 -0
  71. package/lib/plugins/toolbar/default-toolbar.js +229 -0
  72. package/lib/plugins/toolbar/done-button.js +53 -0
  73. package/lib/plugins/toolbar/editor-and-toolbar.js +286 -0
  74. package/lib/plugins/toolbar/index.js +34 -0
  75. package/lib/plugins/toolbar/toolbar-buttons.js +194 -0
  76. package/lib/plugins/toolbar/toolbar.js +376 -0
  77. package/lib/plugins/utils.js +62 -0
  78. package/lib/serialization.js +677 -0
  79. package/lib/shared/alert-dialog.js +75 -0
  80. package/lib/theme.js +9 -0
  81. package/package.json +69 -0
  82. package/src/__tests__/editor.test.jsx +363 -0
  83. package/src/__tests__/serialization.test.js +291 -0
  84. package/src/__tests__/utils.js +36 -0
  85. package/src/block-tags.js +17 -0
  86. package/src/constants.js +7 -0
  87. package/src/editor.jsx +1197 -0
  88. package/src/extensions/characters.js +46 -0
  89. package/src/extensions/component.jsx +294 -0
  90. package/src/extensions/css.js +217 -0
  91. package/src/extensions/custom-toolbar-wrapper.jsx +100 -0
  92. package/src/extensions/image.js +55 -0
  93. package/src/extensions/math.js +259 -0
  94. package/src/extensions/media.js +182 -0
  95. package/src/extensions/responseArea.js +205 -0
  96. package/src/index.jsx +1462 -0
  97. package/src/old-index.jsx +162 -0
  98. package/src/parse-html.js +8 -0
  99. package/src/plugins/README.md +27 -0
  100. package/src/plugins/characters/custom-popper.js +48 -0
  101. package/src/plugins/characters/index.jsx +284 -0
  102. package/src/plugins/characters/utils.js +447 -0
  103. package/src/plugins/css/icons/index.jsx +17 -0
  104. package/src/plugins/css/index.jsx +340 -0
  105. package/src/plugins/customPlugin/index.jsx +85 -0
  106. package/src/plugins/html/icons/index.jsx +19 -0
  107. package/src/plugins/html/index.jsx +72 -0
  108. package/src/plugins/image/__tests__/__snapshots__/component.test.jsx.snap +51 -0
  109. package/src/plugins/image/__tests__/__snapshots__/image-toolbar-logic.test.jsx.snap +27 -0
  110. package/src/plugins/image/__tests__/__snapshots__/image-toolbar.test.jsx.snap +44 -0
  111. package/src/plugins/image/__tests__/component.test.jsx +41 -0
  112. package/src/plugins/image/__tests__/image-toolbar-logic.test.jsx +42 -0
  113. package/src/plugins/image/__tests__/image-toolbar.test.jsx +11 -0
  114. package/src/plugins/image/__tests__/index.test.js +95 -0
  115. package/src/plugins/image/__tests__/insert-image-handler.test.js +113 -0
  116. package/src/plugins/image/__tests__/mock-change.js +15 -0
  117. package/src/plugins/image/alt-dialog.jsx +82 -0
  118. package/src/plugins/image/component.jsx +343 -0
  119. package/src/plugins/image/image-toolbar.jsx +100 -0
  120. package/src/plugins/image/index.jsx +227 -0
  121. package/src/plugins/image/insert-image-handler.js +79 -0
  122. package/src/plugins/index.jsx +377 -0
  123. package/src/plugins/list/__tests__/index.test.js +54 -0
  124. package/src/plugins/list/index.jsx +305 -0
  125. package/src/plugins/math/__tests__/__snapshots__/index.test.jsx.snap +48 -0
  126. package/src/plugins/math/__tests__/index.test.jsx +245 -0
  127. package/src/plugins/math/index.jsx +379 -0
  128. package/src/plugins/media/__tests__/index.test.js +75 -0
  129. package/src/plugins/media/index.jsx +325 -0
  130. package/src/plugins/media/media-dialog.js +624 -0
  131. package/src/plugins/media/media-toolbar.jsx +56 -0
  132. package/src/plugins/media/media-wrapper.jsx +43 -0
  133. package/src/plugins/rendering/index.js +31 -0
  134. package/src/plugins/respArea/drag-in-the-blank/choice.jsx +215 -0
  135. package/src/plugins/respArea/drag-in-the-blank/index.jsx +70 -0
  136. package/src/plugins/respArea/explicit-constructed-response/index.jsx +92 -0
  137. package/src/plugins/respArea/icons/index.jsx +71 -0
  138. package/src/plugins/respArea/index.jsx +299 -0
  139. package/src/plugins/respArea/inline-dropdown/index.jsx +108 -0
  140. package/src/plugins/respArea/math-templated/index.jsx +104 -0
  141. package/src/plugins/respArea/utils.jsx +90 -0
  142. package/src/plugins/table/CustomTablePlugin.js +113 -0
  143. package/src/plugins/table/__tests__/__snapshots__/table-toolbar.test.jsx.snap +44 -0
  144. package/src/plugins/table/__tests__/index.test.jsx +401 -0
  145. package/src/plugins/table/__tests__/table-toolbar.test.jsx +42 -0
  146. package/src/plugins/table/icons/index.jsx +53 -0
  147. package/src/plugins/table/index.jsx +427 -0
  148. package/src/plugins/table/table-toolbar.jsx +136 -0
  149. package/src/plugins/textAlign/icons/index.jsx +114 -0
  150. package/src/plugins/textAlign/index.jsx +23 -0
  151. package/src/plugins/toolbar/__tests__/__snapshots__/default-toolbar.test.jsx.snap +923 -0
  152. package/src/plugins/toolbar/__tests__/__snapshots__/editor-and-toolbar.test.jsx.snap +20 -0
  153. package/src/plugins/toolbar/__tests__/__snapshots__/toolbar-buttons.test.jsx.snap +36 -0
  154. package/src/plugins/toolbar/__tests__/__snapshots__/toolbar.test.jsx.snap +46 -0
  155. package/src/plugins/toolbar/__tests__/default-toolbar.test.jsx +94 -0
  156. package/src/plugins/toolbar/__tests__/editor-and-toolbar.test.jsx +37 -0
  157. package/src/plugins/toolbar/__tests__/toolbar-buttons.test.jsx +51 -0
  158. package/src/plugins/toolbar/__tests__/toolbar.test.jsx +106 -0
  159. package/src/plugins/toolbar/default-toolbar.jsx +206 -0
  160. package/src/plugins/toolbar/done-button.jsx +38 -0
  161. package/src/plugins/toolbar/editor-and-toolbar.jsx +257 -0
  162. package/src/plugins/toolbar/index.jsx +23 -0
  163. package/src/plugins/toolbar/toolbar-buttons.jsx +138 -0
  164. package/src/plugins/toolbar/toolbar.jsx +338 -0
  165. package/src/plugins/utils.js +31 -0
  166. package/src/serialization.jsx +621 -0
  167. package/src/theme.js +1 -0
@@ -0,0 +1,259 @@
1
+ import React, { useState, useEffect, useRef } from 'react';
2
+ import { Extension, Node, mergeAttributes } from '@tiptap/core';
3
+ import { NodeViewWrapper, ReactRenderer, ReactNodeViewRenderer } from '@tiptap/react';
4
+ import { Plugin, PluginKey, NodeSelection, TextSelection } from 'prosemirror-state';
5
+ import { MathPreview, MathToolbar } from '@pie-lib/math-toolbar';
6
+ import { wrapMath, mmlToLatex, renderMath } from '@pie-lib/math-rendering';
7
+
8
+ const ensureTextAfterMathPluginKey = new PluginKey('ensureTextAfterMath');
9
+
10
+ export const EnsureTextAfterMathPlugin = (mathNodeName) =>
11
+ new Plugin({
12
+ key: ensureTextAfterMathPluginKey,
13
+ appendTransaction: (transactions, oldState, newState) => {
14
+ // Only act when the doc actually changed
15
+ if (!transactions.some((tr) => tr.docChanged)) return null;
16
+
17
+ const tr = newState.tr;
18
+ let changed = false;
19
+
20
+ newState.doc.descendants((node, pos) => {
21
+ if (node.type.name === mathNodeName) {
22
+ const nextPos = pos + node.nodeSize;
23
+ const nextNode = newState.doc.nodeAt(nextPos);
24
+
25
+ // If there's no node after, or the next node isn't text, insert a space
26
+ if (!nextNode || nextNode.type.name !== 'text') {
27
+ tr.insert(nextPos, newState.schema.text('\u200b'));
28
+ changed = true;
29
+ }
30
+ }
31
+ });
32
+
33
+ return changed ? tr : null;
34
+ },
35
+ });
36
+
37
+ export const ZeroWidthSpaceHandlingPlugin = new Plugin({
38
+ key: new PluginKey('zeroWidthSpaceHandling'),
39
+ props: {
40
+ handleKeyDown(view, event) {
41
+ const { state, dispatch } = view;
42
+ const { selection, doc } = state;
43
+ const { from, empty } = selection;
44
+
45
+ if (empty && event.key === 'Backspace' && from > 0) {
46
+ const prevChar = doc.textBetween(from - 1, from, '\uFFFC', '\uFFFC');
47
+ if (prevChar === '\u200b') {
48
+ const tr = state.tr.delete(from - 2, from);
49
+ dispatch(tr);
50
+ return true; // handled
51
+ }
52
+ }
53
+
54
+ if (empty && event.key === 'ArrowLeft' && from > 0) {
55
+ const prevChar = doc.textBetween(from - 1, from, '\uFFFC', '\uFFFC');
56
+ // If the previous character is the zero-width space...
57
+ if (prevChar === '\u200b') {
58
+ const posBefore = from - 1;
59
+ const resolved = state.doc.resolve(posBefore - 1); // look just before the zwsp
60
+ const maybeNode = resolved.nodeAfter || resolved.nodeBefore;
61
+
62
+ // Check if there's an inline selectable node (e.g., your math node)
63
+ if (maybeNode) {
64
+ const nodePos = posBefore - maybeNode.nodeSize;
65
+ const nodeResolved = state.doc.resolve(nodePos);
66
+ const tr = state.tr.setSelection(NodeSelection.create(state.doc, nodeResolved.pos));
67
+ dispatch(tr);
68
+ return true;
69
+ } else {
70
+ // Just move the text cursor before the zwsp
71
+ const tr = state.tr.setSelection(TextSelection.create(state.doc, from - 2));
72
+ dispatch(tr);
73
+ return true;
74
+ }
75
+ }
76
+ }
77
+
78
+ return false;
79
+ },
80
+ },
81
+ });
82
+
83
+ export const MathNode = Node.create({
84
+ name: 'math',
85
+ group: 'inline',
86
+ inline: true,
87
+ atom: true,
88
+
89
+ addAttributes() {
90
+ return {
91
+ latex: { default: '' },
92
+ wrapper: { default: null },
93
+ html: { default: null },
94
+ };
95
+ },
96
+
97
+ addProseMirrorPlugins() {
98
+ return [EnsureTextAfterMathPlugin(this.name), ZeroWidthSpaceHandlingPlugin];
99
+ },
100
+
101
+ parseHTML() {
102
+ return [
103
+ {
104
+ tag: 'span[data-latex]',
105
+ getAttrs: (el) => ({
106
+ latex: el.getAttribute('data-raw') || el.textContent,
107
+ }),
108
+ },
109
+ {
110
+ tag: 'span[data-type="mathml"]',
111
+ getAttrs: (el) => ({
112
+ html: el.innerHTML,
113
+ }),
114
+ },
115
+ ];
116
+ },
117
+
118
+ addCommands() {
119
+ return {
120
+ insertMath: (latex = '') => ({ tr, editor, dispatch }) => {
121
+ // 2) Now the editor.view.state reflects the insertion
122
+ const { state } = editor.view;
123
+ const node = state.schema.nodes.math.create({
124
+ latex,
125
+ });
126
+ const { selection } = state;
127
+
128
+ // The inserted node is typically just before the cursor
129
+ const pos = selection.$from.pos;
130
+
131
+ tr.insert(pos, node);
132
+
133
+ if (node?.type?.name === this.name) {
134
+ // Create a NodeSelection from the current doc
135
+ const sel = NodeSelection.create(tr.doc, selection.$from.pos);
136
+
137
+ // Build a fresh transaction from the current state and set the selection
138
+ tr.setSelection(sel);
139
+ }
140
+
141
+ dispatch(tr);
142
+
143
+ return true;
144
+ },
145
+ // insertMath: (latex = '') => ({ commands }) => {
146
+ // return commands.insertContent({
147
+ // type: this.name,
148
+ // attrs: { latex },
149
+ // });
150
+ // },
151
+ };
152
+ },
153
+
154
+ renderHTML({ HTMLAttributes }) {
155
+ if (HTMLAttributes.html) {
156
+ return ['span', { 'data-type': 'mathml', dangerouslySetInnerHTML: { __html: HTMLAttributes.html } }];
157
+ }
158
+
159
+ return [
160
+ 'span',
161
+ { 'data-latex': '', 'data-raw': HTMLAttributes.latex },
162
+ wrapMath(HTMLAttributes.latex, HTMLAttributes.wrapper),
163
+ ];
164
+ },
165
+
166
+ addNodeView() {
167
+ return ReactNodeViewRenderer((props) => <MathNodeView {...{ ...props, options: this.options }} />);
168
+ },
169
+ });
170
+
171
+ export const MathNodeView = (props) => {
172
+ const { node, updateAttributes, editor, selected, options } = props;
173
+ const [showToolbar, setShowToolbar] = useState(selected);
174
+ const toolbarRef = useRef(null);
175
+
176
+ const latex = node.attrs.latex || '';
177
+
178
+ useEffect(() => {
179
+ if (selected) {
180
+ setShowToolbar(true);
181
+ }
182
+ }, [selected]);
183
+
184
+ useEffect(() => {
185
+ editor._toolbarOpened = !!showToolbar;
186
+ }, [showToolbar]);
187
+
188
+ useEffect(() => {
189
+ const handleClickOutside = (event) => {
190
+ if (
191
+ toolbarRef.current &&
192
+ !toolbarRef.current.contains(event.target) &&
193
+ !event.target.closest('[data-inline-node]')
194
+ ) {
195
+ setShowToolbar(false);
196
+ }
197
+ };
198
+
199
+ if (showToolbar) {
200
+ document.addEventListener('mousedown', handleClickOutside);
201
+ } else {
202
+ document.removeEventListener('mousedown', handleClickOutside);
203
+ }
204
+
205
+ return () => document.removeEventListener('mousedown', handleClickOutside);
206
+ }, [showToolbar]);
207
+
208
+ const handleChange = (newLatex) => {
209
+ updateAttributes({ latex: newLatex });
210
+ };
211
+
212
+ const handleDone = (newLatex) => {
213
+ updateAttributes({ latex: newLatex });
214
+ setShowToolbar(false);
215
+
216
+ editor._toolbarOpened = false;
217
+
218
+ const { selection, tr, doc } = editor.state;
219
+ const sel = TextSelection.create(doc, selection.from + 1);
220
+
221
+ // Build a fresh transaction from the current state and set the selection
222
+ tr.setSelection(sel);
223
+ editor.view.dispatch(tr);
224
+ editor.commands.focus();
225
+ };
226
+
227
+ return (
228
+ <NodeViewWrapper
229
+ className="math-node"
230
+ style={{
231
+ display: 'inline-flex',
232
+ cursor: 'pointer',
233
+ margin: '0 4px',
234
+ }}
235
+ data-selected={selected}
236
+ >
237
+ <div onClick={() => setShowToolbar(true)} contentEditable={false}>
238
+ <MathPreview latex={latex} />
239
+ </div>
240
+
241
+ {showToolbar && (
242
+ <div
243
+ ref={toolbarRef}
244
+ style={{
245
+ position: 'absolute',
246
+ top: '100%',
247
+ left: 0,
248
+ zIndex: 20,
249
+ background: 'var(--editable-html-toolbar-bg, #efefef)',
250
+ boxShadow:
251
+ '0px 1px 5px 0px rgba(0, 0, 0, 0.2), 0px 2px 2px 0px rgba(0, 0, 0, 0.14), 0px 3px 1px -2px rgba(0, 0, 0, 0.12)',
252
+ }}
253
+ >
254
+ <MathToolbar latex={latex} autoFocus onChange={handleChange} onDone={handleDone} keypadMode="basic" />
255
+ </div>
256
+ )}
257
+ </NodeViewWrapper>
258
+ );
259
+ };
@@ -0,0 +1,182 @@
1
+ import React, { useEffect } from 'react';
2
+ import ReactDOM from 'react-dom';
3
+ import { mergeAttributes, Node } from '@tiptap/core';
4
+ import { NodeViewWrapper, ReactNodeViewRenderer } from '@tiptap/react';
5
+ import MediaDialog from '../plugins/media/media-dialog';
6
+ import MediaToolbar from '../plugins/media/media-toolbar';
7
+
8
+ export const Media = Node.create({
9
+ name: 'media',
10
+ group: 'inline',
11
+ inline: true,
12
+ atom: true,
13
+
14
+ addAttributes() {
15
+ return {
16
+ type: { default: 'video' },
17
+ src: { default: null },
18
+ width: { default: null },
19
+ height: { default: null },
20
+ title: { default: null },
21
+ starts: { default: null },
22
+ ends: { default: null },
23
+ editing: { default: false },
24
+ tag: { default: 'iframe' }, // 'iframe' or 'audio'
25
+ url: { default: null },
26
+ };
27
+ },
28
+
29
+ parseHTML() {
30
+ return [
31
+ {
32
+ tag: 'iframe[data-type="video"]',
33
+ getAttrs: (el) => ({
34
+ type: 'video',
35
+ tag: 'iframe',
36
+ src: el.getAttribute('src'),
37
+ width: el.getAttribute('width'),
38
+ height: el.getAttribute('height'),
39
+ title: el.dataset.title,
40
+ starts: el.dataset.starts,
41
+ ends: el.dataset.ends,
42
+ url: el.dataset.url,
43
+ }),
44
+ },
45
+ {
46
+ tag: 'audio',
47
+ getAttrs: (el) => ({
48
+ type: 'audio',
49
+ tag: 'audio',
50
+ src: el.querySelector('source')?.getAttribute('src'),
51
+ }),
52
+ },
53
+ ];
54
+ },
55
+
56
+ renderHTML({ HTMLAttributes }) {
57
+ const { tag, src, width, height } = HTMLAttributes;
58
+
59
+ if (tag === 'audio') {
60
+ return ['audio', { controls: 'controls', controlsList: 'nodownload' }, ['source', { src, type: 'audio/mp3' }]];
61
+ }
62
+
63
+ return [
64
+ 'iframe',
65
+ mergeAttributes(
66
+ {
67
+ 'data-type': 'video',
68
+ frameborder: '0',
69
+ allow: 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture',
70
+ allowfullscreen: '',
71
+ src,
72
+ },
73
+ width ? { width } : {},
74
+ height ? { height } : {},
75
+ ),
76
+ ];
77
+ },
78
+
79
+ addCommands() {
80
+ return {
81
+ insertMedia: (attrs) => ({ commands }) => {
82
+ return commands.insertContent({ type: this.name, attrs });
83
+ },
84
+ updateMedia: (attrs) => ({ commands }) => {
85
+ return commands.updateAttributes(this.name, attrs);
86
+ },
87
+ };
88
+ },
89
+
90
+ addNodeView() {
91
+ return ReactNodeViewRenderer((props) => <MediaNodeView {...{ ...props, options: this.options }} />);
92
+ },
93
+ });
94
+
95
+ const removeDialogs = () => {
96
+ const prevDialogs = document.querySelectorAll('.insert-media-dialog');
97
+
98
+ prevDialogs.forEach((s) => s.remove());
99
+ };
100
+
101
+ export const insertDialog = (props) => {
102
+ const newEl = document.createElement('div');
103
+ const { type, callback, options, ...rest } = props;
104
+ const initialBodyOverflow = document.body.style.overflow;
105
+
106
+ removeDialogs();
107
+
108
+ newEl.className = 'insert-media-dialog';
109
+ document.body.style.overflow = 'hidden';
110
+
111
+ const handleClose = (val, data) => {
112
+ callback(val, data);
113
+ newEl.remove();
114
+ document.body.style.overflow = initialBodyOverflow;
115
+ };
116
+
117
+ const el = (
118
+ <MediaDialog
119
+ {...rest}
120
+ uploadSoundSupport={options.uploadSoundSupport}
121
+ type={type}
122
+ disablePortal={true}
123
+ open={true}
124
+ handleClose={handleClose}
125
+ />
126
+ );
127
+
128
+ ReactDOM.render(el, newEl);
129
+
130
+ document.body.appendChild(newEl);
131
+ };
132
+
133
+ export default function MediaNodeView({ editor, node, updateAttributes, deleteNode, options }) {
134
+ const { type, src, width, height, tag } = node.attrs;
135
+
136
+ const handleEdit = () => {
137
+ insertDialog({
138
+ ...node.attrs,
139
+ edit: true,
140
+ callback: (val, data) => {
141
+ if (val) {
142
+ updateAttributes(data);
143
+ } else {
144
+ deleteNode();
145
+ }
146
+
147
+ editor.chain().focus().run();
148
+ },
149
+ });
150
+ };
151
+
152
+ useEffect(() => {
153
+ insertDialog({
154
+ ...node.attrs,
155
+ options: options,
156
+ edit: true,
157
+ callback: (val, data) => {
158
+ if (val) {
159
+ updateAttributes(data);
160
+ } else {
161
+ deleteNode();
162
+ }
163
+
164
+ editor.chain().focus().run();
165
+ },
166
+ });
167
+ }, []);
168
+
169
+ return (
170
+ <NodeViewWrapper data-type={type} style={{ width, height }}>
171
+ {tag === 'audio' ? (
172
+ <audio controls controlsList="nodownload">
173
+ <source src={src} />
174
+ </audio>
175
+ ) : (
176
+ <iframe src={src} allowFullScreen frameBorder="0" />
177
+ )}
178
+
179
+ <MediaToolbar onEdit={handleEdit} onRemove={deleteNode} />
180
+ </NodeViewWrapper>
181
+ );
182
+ }
@@ -0,0 +1,205 @@
1
+ // InlineNodes.js
2
+ import React from 'react';
3
+ import { Node, ReactNodeViewRenderer, ReactRenderer } from "@tiptap/react";
4
+ import ExplicitConstructedResponse from '../plugins/respArea/explicit-constructed-response';
5
+ import DragInTheBlank from '../plugins/respArea/drag-in-the-blank';
6
+ import InlineDropdown from '../plugins/respArea/inline-dropdown';
7
+ import { Extension } from "@tiptap/core";
8
+ import { MathToolbar } from "@pie-lib/math-toolbar";
9
+ import tippy from "tippy.js";
10
+
11
+ export const ResponseAreaExtension = Extension.create({
12
+ name: 'responseArea',
13
+ addCommands() {
14
+ return {
15
+ insertResponseArea: (type) => ({ tr, state, dispatch }) => {
16
+ const { schema, selection } = state;
17
+ const position = selection.$from.pos;
18
+ const RESP_MAP = {
19
+ 'drag-in-the-blank': 'drag_in_the_blank',
20
+ 'explicit-constructed-response': 'explicit_constructed_response',
21
+ 'inline-dropdown': 'inline_dropdown',
22
+ };
23
+
24
+ const node = schema.nodes[RESP_MAP[type]].create({
25
+ index: '1',
26
+ id: '1',
27
+ value: '',
28
+ });
29
+
30
+ if (dispatch) {
31
+ tr.insert(position, node);
32
+ dispatch(tr);
33
+ }
34
+
35
+ return true;
36
+ },
37
+ };
38
+ },
39
+ });
40
+
41
+ /**
42
+ * ExplicitConstructedResponse Node
43
+ */
44
+ export const ExplicitConstructedResponseNode = Node.create({
45
+ name: 'explicit_constructed_response',
46
+ group: 'inline',
47
+ inline: true,
48
+ atom: true,
49
+ addAttributes() {
50
+ return {
51
+ index: { default: null },
52
+ value: { default: '' },
53
+ };
54
+ },
55
+ parseHTML() {
56
+ return [
57
+ {
58
+ tag: 'span[data-type="explicit_constructed_response"]',
59
+ getAttrs: (el) => ({
60
+ index: el.dataset.index,
61
+ value: el.dataset.value,
62
+ }),
63
+ },
64
+ ];
65
+ },
66
+ renderHTML({ HTMLAttributes }) {
67
+ return [
68
+ 'span',
69
+ {
70
+ 'data-type': 'explicit_constructed_response',
71
+ 'data-index': HTMLAttributes.index,
72
+ 'data-value': HTMLAttributes.value,
73
+ },
74
+ ];
75
+ },
76
+ addNodeView() {
77
+ return ReactNodeViewRenderer((props) => <ExplicitConstructedResponse {...{ ...props, options: this.options }} />);
78
+ },
79
+ });
80
+
81
+ /**
82
+ * MathTemplated Node
83
+ */
84
+ export const MathTemplatedNode = Node.create({
85
+ name: 'math_templated',
86
+ group: 'inline',
87
+ inline: true,
88
+ atom: true,
89
+ addAttributes() {
90
+ return {
91
+ index: { default: null },
92
+ value: { default: '' },
93
+ };
94
+ },
95
+ parseHTML() {
96
+ return [
97
+ {
98
+ tag: 'span[data-type="math_templated"]',
99
+ getAttrs: (el) => ({
100
+ index: el.dataset.index,
101
+ value: el.dataset.value,
102
+ }),
103
+ },
104
+ ];
105
+ },
106
+ renderHTML({ HTMLAttributes }) {
107
+ return [
108
+ 'span',
109
+ {
110
+ 'data-type': 'math_templated',
111
+ 'data-index': HTMLAttributes.index,
112
+ 'data-value': HTMLAttributes.value,
113
+ },
114
+ ];
115
+ },
116
+ addNodeView() {
117
+ return ReactNodeViewRenderer(() => <div></div>);
118
+ },
119
+ });
120
+
121
+ /**
122
+ * DragInTheBlank Node
123
+ */
124
+ export const DragInTheBlankNode = Node.create({
125
+ name: 'drag_in_the_blank',
126
+ group: 'inline',
127
+ inline: true,
128
+ atom: true,
129
+ addAttributes() {
130
+ return {
131
+ index: { default: null },
132
+ id: { default: null },
133
+ value: { default: '' },
134
+ inTable: { default: null },
135
+ };
136
+ },
137
+ parseHTML() {
138
+ return [
139
+ {
140
+ tag: 'span[data-type="drag_in_the_blank"]',
141
+ getAttrs: (el) => ({
142
+ index: el.dataset.index,
143
+ id: el.dataset.id,
144
+ value: el.dataset.value,
145
+ inTable: el.dataset.inTable,
146
+ }),
147
+ },
148
+ ];
149
+ },
150
+ renderHTML({ HTMLAttributes }) {
151
+ return [
152
+ 'span',
153
+ {
154
+ 'data-type': 'drag_in_the_blank',
155
+ 'data-index': HTMLAttributes.index,
156
+ 'data-id': HTMLAttributes.id,
157
+ 'data-value': HTMLAttributes.value,
158
+ 'data-in-table': HTMLAttributes.inTable,
159
+ },
160
+ ];
161
+ },
162
+ addNodeView() {
163
+ return ReactNodeViewRenderer((props) => <DragInTheBlank {...{ ...props, options: this.options }} />);
164
+ },
165
+ });
166
+
167
+ /**
168
+ * InlineDropdown Node
169
+ */
170
+ export const InlineDropdownNode = Node.create({
171
+ name: 'inline_dropdown',
172
+ group: 'inline',
173
+ inline: true,
174
+ atom: true,
175
+ addAttributes() {
176
+ return {
177
+ index: { default: null },
178
+ value: { default: '' },
179
+ };
180
+ },
181
+ parseHTML() {
182
+ return [
183
+ {
184
+ tag: 'span[data-type="inline_dropdown"]',
185
+ getAttrs: (el) => ({
186
+ index: el.dataset.index,
187
+ value: el.dataset.value,
188
+ }),
189
+ },
190
+ ];
191
+ },
192
+ renderHTML({ HTMLAttributes }) {
193
+ return [
194
+ 'span',
195
+ {
196
+ 'data-type': 'inline_dropdown',
197
+ 'data-index': HTMLAttributes.index,
198
+ 'data-value': HTMLAttributes.value,
199
+ },
200
+ ];
201
+ },
202
+ addNodeView() {
203
+ return ReactNodeViewRenderer((props) => <InlineDropdown {...{ ...props, options: this.options }} />);
204
+ },
205
+ });