@pie-lib/editable-html-tip-tap 1.0.12 → 1.0.14
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/lib/components/EditableHtml.js +33 -16
- package/lib/components/MenuBar.js +2 -2
- package/lib/components/respArea/ExplicitConstructedResponse.js +15 -4
- package/lib/components/respArea/InlineDropdown.js +25 -4
- package/lib/extensions/index.js +3 -3
- package/lib/extensions/math.js +3 -5
- package/lib/extensions/responseArea.js +211 -33
- package/package.json +6 -6
- package/src/components/EditableHtml.jsx +18 -4
- package/src/components/MenuBar.jsx +8 -6
- package/src/components/respArea/ExplicitConstructedResponse.jsx +13 -2
- package/src/components/respArea/InlineDropdown.jsx +20 -5
- package/src/extensions/index.js +4 -4
- package/src/extensions/math.js +1 -2
- package/src/extensions/responseArea.js +186 -15
|
@@ -1,31 +1,202 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { Plugin, PluginKey, TextSelection } from 'prosemirror-state';
|
|
3
|
+
import { Extension } from '@tiptap/core';
|
|
4
|
+
import { Node, ReactNodeViewRenderer } from '@tiptap/react';
|
|
3
5
|
import ExplicitConstructedResponse from '../components/respArea/ExplicitConstructedResponse';
|
|
4
6
|
import DragInTheBlank from '../components/respArea/DragInTheBlank/DragInTheBlank';
|
|
5
7
|
import InlineDropdown from '../components/respArea/InlineDropdown';
|
|
6
|
-
import
|
|
8
|
+
import PropTypes from 'prop-types';
|
|
9
|
+
|
|
10
|
+
const lastIndexMap = {};
|
|
11
|
+
|
|
12
|
+
const normalizeType = (type) => String(type || '').replace(/-/g, '_');
|
|
13
|
+
|
|
14
|
+
const getAttrIndex = (node) => (node && node.attrs && node.attrs.index != null ? String(node.attrs.index) : null);
|
|
15
|
+
|
|
16
|
+
const collectNodesOfType = (doc, typeName) => {
|
|
17
|
+
const results = [];
|
|
18
|
+
|
|
19
|
+
doc.descendants((node, pos) => {
|
|
20
|
+
if (node.type && node.type.name === typeName) {
|
|
21
|
+
const index = getAttrIndex(node);
|
|
22
|
+
if (index != null) results.push({ index, pos, node });
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return results;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const countNodesOfType = (doc, typeName) => {
|
|
31
|
+
let count = 0;
|
|
32
|
+
doc.descendants((node) => {
|
|
33
|
+
if (node.type && node.type.name === typeName) count += 1;
|
|
34
|
+
return true;
|
|
35
|
+
});
|
|
36
|
+
return count;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const getDefaultNode = ({ schema, typeName, index }) => {
|
|
40
|
+
const nodeType = schema.nodes[typeName];
|
|
41
|
+
if (!nodeType) return null;
|
|
42
|
+
|
|
43
|
+
// mirror your Slate "getDefaultElement(opts, newIndex)"
|
|
44
|
+
// customize attrs as needed:
|
|
45
|
+
return nodeType.create({
|
|
46
|
+
index: String(index),
|
|
47
|
+
id: String(index),
|
|
48
|
+
value: '',
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Find a good cursor position *after* an inserted node.
|
|
53
|
+
const selectionAfterPos = (doc, pos) => {
|
|
54
|
+
const $pos = doc.resolve(Math.min(pos, doc.content.size));
|
|
55
|
+
return TextSelection.near($pos, 1);
|
|
56
|
+
};
|
|
7
57
|
|
|
8
58
|
export const ResponseAreaExtension = Extension.create({
|
|
9
59
|
name: 'responseArea',
|
|
60
|
+
|
|
61
|
+
addOptions() {
|
|
62
|
+
return {
|
|
63
|
+
maxResponseAreas: null,
|
|
64
|
+
error: null,
|
|
65
|
+
options: null,
|
|
66
|
+
respAreaToolbar: null,
|
|
67
|
+
onHandleAreaChange: null,
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
addProseMirrorPlugins() {
|
|
72
|
+
if (!this.options.type) {
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const typeName = normalizeType(this.options.type);
|
|
77
|
+
const key = new PluginKey(`response-area-watcher:${typeName}`);
|
|
78
|
+
|
|
79
|
+
return [
|
|
80
|
+
new Plugin({
|
|
81
|
+
key,
|
|
82
|
+
|
|
83
|
+
view: (view) => {
|
|
84
|
+
// Lazy init lastIndexMap[typeName]
|
|
85
|
+
if (lastIndexMap[typeName] === undefined) {
|
|
86
|
+
lastIndexMap[typeName] = 0;
|
|
87
|
+
|
|
88
|
+
view.state.doc.descendants((node) => {
|
|
89
|
+
if (node.type && node.type.name === typeName) {
|
|
90
|
+
const idx = getAttrIndex(node);
|
|
91
|
+
if (idx != null) {
|
|
92
|
+
const n = parseInt(idx, 10);
|
|
93
|
+
if (!Number.isNaN(n) && n > lastIndexMap[typeName]) {
|
|
94
|
+
lastIndexMap[typeName] = n;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
update: (view, prevState) => {
|
|
104
|
+
const state = view.state;
|
|
105
|
+
if (prevState.doc.eq(state.doc)) return;
|
|
106
|
+
|
|
107
|
+
const currentList = collectNodesOfType(state.doc, typeName);
|
|
108
|
+
const oldList = collectNodesOfType(prevState.doc, typeName);
|
|
109
|
+
|
|
110
|
+
if (this.options.toolbar) {
|
|
111
|
+
this.options.toolbar.disabled = currentList.length >= this.options.maxResponseAreas;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Removed elements (same logic as Slate)
|
|
115
|
+
if (oldList.length > currentList.length) {
|
|
116
|
+
const currentIndexSet = new Set(currentList.map((x) => x.index));
|
|
117
|
+
|
|
118
|
+
const removed = oldList.filter((x) => !currentIndexSet.has(x.index));
|
|
119
|
+
|
|
120
|
+
if (removed.length && typeof this.options.onHandleAreaChange === 'function') {
|
|
121
|
+
this.options.onHandleAreaChange(removed);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
}),
|
|
128
|
+
];
|
|
129
|
+
},
|
|
130
|
+
|
|
10
131
|
addCommands() {
|
|
11
132
|
return {
|
|
12
|
-
insertResponseArea: (type) => ({ tr, state, dispatch }) => {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
133
|
+
insertResponseArea: (type) => ({ tr, state, dispatch, commands }) => {
|
|
134
|
+
const typeName = normalizeType(type);
|
|
135
|
+
|
|
136
|
+
// --- Slate: currentRespAreaList + max check ---
|
|
137
|
+
const currentCount = countNodesOfType(state.doc, typeName);
|
|
138
|
+
if (currentCount >= this.options.maxResponseAreas) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// --- Slate: indexing logic (kept identical) ---
|
|
143
|
+
if (lastIndexMap[typeName] === undefined) lastIndexMap[typeName] = 0;
|
|
144
|
+
|
|
145
|
+
const prevIndex = lastIndexMap[typeName];
|
|
146
|
+
const newIndex = prevIndex === 0 ? prevIndex : prevIndex + 1;
|
|
20
147
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
148
|
+
// Slate increments map even if newIndex === 0
|
|
149
|
+
lastIndexMap[typeName] += 1;
|
|
150
|
+
|
|
151
|
+
const newInline = getDefaultNode({
|
|
152
|
+
schema: state.schema,
|
|
153
|
+
typeName,
|
|
154
|
+
index: newIndex,
|
|
25
155
|
});
|
|
26
156
|
|
|
157
|
+
if (!newInline) return false;
|
|
158
|
+
|
|
159
|
+
// --- Insert logic ---
|
|
160
|
+
const { selection } = state;
|
|
161
|
+
let insertPos = selection.from;
|
|
162
|
+
|
|
163
|
+
// If we're in a NodeSelection, insert before/after is ambiguous;
|
|
164
|
+
// We'll insert at its "from" (like your current code).
|
|
165
|
+
// If insertion fails, we fallback to end of doc.
|
|
166
|
+
const tryInsertAt = (pos) => {
|
|
167
|
+
try {
|
|
168
|
+
tr.insert(pos, newInline);
|
|
169
|
+
return pos;
|
|
170
|
+
} catch (e) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
let usedPos = tryInsertAt(insertPos);
|
|
176
|
+
|
|
177
|
+
// Slate branch: "markup empty and there's no focus"
|
|
178
|
+
// ProseMirror doesn't expose "no focus" the same way, so the closest
|
|
179
|
+
// equivalent fallback is inserting at end of document.
|
|
180
|
+
if (usedPos == null) {
|
|
181
|
+
usedPos = tryInsertAt(tr.doc.content.size);
|
|
182
|
+
}
|
|
183
|
+
if (usedPos == null) return false;
|
|
184
|
+
|
|
185
|
+
// Optionally select the node you just inserted (like your original command)
|
|
186
|
+
// tr.setSelection(NodeSelection.create(tr.doc, usedPos))
|
|
187
|
+
|
|
188
|
+
// --- Cursor move behavior for certain types (Slate: moveFocusTo next text) ---
|
|
189
|
+
if (typeName === 'drag_in_the_blank' || typeName === 'math_templated') {
|
|
190
|
+
const after = usedPos + newInline.nodeSize;
|
|
191
|
+
tr.setSelection(selectionAfterPos(tr.doc, after));
|
|
192
|
+
} else {
|
|
193
|
+
// Default: put cursor after inserted node
|
|
194
|
+
const after = usedPos + newInline.nodeSize;
|
|
195
|
+
tr.setSelection(selectionAfterPos(tr.doc, after));
|
|
196
|
+
}
|
|
197
|
+
|
|
27
198
|
if (dispatch) {
|
|
28
|
-
|
|
199
|
+
commands.focus();
|
|
29
200
|
dispatch(tr);
|
|
30
201
|
}
|
|
31
202
|
|