@pie-lib/editable-html 11.1.1 → 11.2.1-beta.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.
- package/CHANGELOG.md +43 -167
- package/NEXT.CHANGELOG.json +1 -0
- package/package.json +10 -6
- package/src/__tests__/editor.test.jsx +363 -0
- package/src/__tests__/serialization.test.js +291 -0
- package/src/__tests__/utils.js +36 -0
- package/src/block-tags.js +17 -0
- package/src/constants.js +7 -0
- package/src/editor.jsx +303 -49
- package/src/index.jsx +19 -10
- package/src/plugins/characters/index.jsx +11 -3
- package/src/plugins/characters/utils.js +12 -12
- package/src/plugins/css/icons/index.jsx +17 -0
- package/src/plugins/css/index.jsx +346 -0
- package/src/plugins/customPlugin/index.jsx +85 -0
- package/src/plugins/html/index.jsx +9 -6
- package/src/plugins/image/__tests__/__snapshots__/component.test.jsx.snap +51 -0
- package/src/plugins/image/__tests__/__snapshots__/image-toolbar-logic.test.jsx.snap +27 -0
- package/src/plugins/image/__tests__/__snapshots__/image-toolbar.test.jsx.snap +44 -0
- package/src/plugins/image/__tests__/component.test.jsx +41 -0
- package/src/plugins/image/__tests__/image-toolbar-logic.test.jsx +42 -0
- package/src/plugins/image/__tests__/image-toolbar.test.jsx +11 -0
- package/src/plugins/image/__tests__/index.test.js +95 -0
- package/src/plugins/image/__tests__/insert-image-handler.test.js +113 -0
- package/src/plugins/image/__tests__/mock-change.js +15 -0
- package/src/plugins/image/index.jsx +2 -1
- package/src/plugins/image/insert-image-handler.js +13 -6
- package/src/plugins/index.jsx +248 -5
- package/src/plugins/list/__tests__/index.test.js +54 -0
- package/src/plugins/list/index.jsx +130 -0
- package/src/plugins/math/__tests__/__snapshots__/index.test.jsx.snap +48 -0
- package/src/plugins/math/__tests__/index.test.jsx +245 -0
- package/src/plugins/math/index.jsx +87 -56
- package/src/plugins/media/__tests__/index.test.js +75 -0
- package/src/plugins/media/index.jsx +3 -2
- package/src/plugins/media/media-dialog.js +106 -57
- package/src/plugins/rendering/index.js +31 -0
- package/src/plugins/respArea/drag-in-the-blank/choice.jsx +4 -1
- package/src/plugins/respArea/explicit-constructed-response/index.jsx +10 -8
- package/src/plugins/respArea/index.jsx +53 -7
- package/src/plugins/respArea/inline-dropdown/index.jsx +13 -6
- package/src/plugins/respArea/math-templated/index.jsx +104 -0
- package/src/plugins/respArea/utils.jsx +11 -0
- package/src/plugins/table/CustomTablePlugin.js +113 -0
- package/src/plugins/table/__tests__/__snapshots__/table-toolbar.test.jsx.snap +44 -0
- package/src/plugins/table/__tests__/index.test.jsx +401 -0
- package/src/plugins/table/__tests__/table-toolbar.test.jsx +42 -0
- package/src/plugins/table/index.jsx +46 -59
- package/src/plugins/table/table-toolbar.jsx +39 -2
- package/src/plugins/textAlign/icons/index.jsx +139 -0
- package/src/plugins/textAlign/index.jsx +23 -0
- package/src/plugins/toolbar/__tests__/__snapshots__/default-toolbar.test.jsx.snap +923 -0
- package/src/plugins/toolbar/__tests__/__snapshots__/editor-and-toolbar.test.jsx.snap +20 -0
- package/src/plugins/toolbar/__tests__/__snapshots__/toolbar-buttons.test.jsx.snap +36 -0
- package/src/plugins/toolbar/__tests__/__snapshots__/toolbar.test.jsx.snap +46 -0
- package/src/plugins/toolbar/__tests__/default-toolbar.test.jsx +94 -0
- package/src/plugins/toolbar/__tests__/editor-and-toolbar.test.jsx +37 -0
- package/src/plugins/toolbar/__tests__/toolbar-buttons.test.jsx +51 -0
- package/src/plugins/toolbar/__tests__/toolbar.test.jsx +106 -0
- package/src/plugins/toolbar/default-toolbar.jsx +82 -20
- package/src/plugins/toolbar/done-button.jsx +3 -1
- package/src/plugins/toolbar/editor-and-toolbar.jsx +18 -13
- package/src/plugins/toolbar/toolbar-buttons.jsx +52 -11
- package/src/plugins/toolbar/toolbar.jsx +31 -8
- package/src/serialization.jsx +213 -38
- package/README.md +0 -45
- package/deploy.sh +0 -16
- package/lib/editor.js +0 -1094
- package/lib/editor.js.map +0 -1
- package/lib/index.js +0 -253
- package/lib/index.js.map +0 -1
- package/lib/parse-html.js +0 -16
- package/lib/parse-html.js.map +0 -1
- package/lib/plugins/characters/custom-popper.js +0 -73
- package/lib/plugins/characters/custom-popper.js.map +0 -1
- package/lib/plugins/characters/index.js +0 -300
- package/lib/plugins/characters/index.js.map +0 -1
- package/lib/plugins/characters/utils.js +0 -381
- package/lib/plugins/characters/utils.js.map +0 -1
- package/lib/plugins/html/icons/index.js +0 -38
- package/lib/plugins/html/icons/index.js.map +0 -1
- package/lib/plugins/html/index.js +0 -76
- package/lib/plugins/html/index.js.map +0 -1
- package/lib/plugins/image/alt-dialog.js +0 -129
- package/lib/plugins/image/alt-dialog.js.map +0 -1
- package/lib/plugins/image/component.js +0 -419
- package/lib/plugins/image/component.js.map +0 -1
- package/lib/plugins/image/image-toolbar.js +0 -177
- package/lib/plugins/image/image-toolbar.js.map +0 -1
- package/lib/plugins/image/index.js +0 -262
- package/lib/plugins/image/index.js.map +0 -1
- package/lib/plugins/image/insert-image-handler.js +0 -152
- package/lib/plugins/image/insert-image-handler.js.map +0 -1
- package/lib/plugins/index.js +0 -143
- package/lib/plugins/index.js.map +0 -1
- package/lib/plugins/list/index.js +0 -204
- package/lib/plugins/list/index.js.map +0 -1
- package/lib/plugins/math/index.js +0 -419
- package/lib/plugins/math/index.js.map +0 -1
- package/lib/plugins/media/index.js +0 -384
- package/lib/plugins/media/index.js.map +0 -1
- package/lib/plugins/media/media-dialog.js +0 -668
- package/lib/plugins/media/media-dialog.js.map +0 -1
- package/lib/plugins/media/media-toolbar.js +0 -101
- package/lib/plugins/media/media-toolbar.js.map +0 -1
- package/lib/plugins/media/media-wrapper.js +0 -93
- package/lib/plugins/media/media-wrapper.js.map +0 -1
- package/lib/plugins/respArea/drag-in-the-blank/choice.js +0 -251
- package/lib/plugins/respArea/drag-in-the-blank/choice.js.map +0 -1
- package/lib/plugins/respArea/drag-in-the-blank/index.js +0 -97
- package/lib/plugins/respArea/drag-in-the-blank/index.js.map +0 -1
- package/lib/plugins/respArea/explicit-constructed-response/index.js +0 -55
- package/lib/plugins/respArea/explicit-constructed-response/index.js.map +0 -1
- package/lib/plugins/respArea/icons/index.js +0 -95
- package/lib/plugins/respArea/icons/index.js.map +0 -1
- package/lib/plugins/respArea/index.js +0 -293
- package/lib/plugins/respArea/index.js.map +0 -1
- package/lib/plugins/respArea/inline-dropdown/index.js +0 -70
- package/lib/plugins/respArea/inline-dropdown/index.js.map +0 -1
- package/lib/plugins/respArea/utils.js +0 -110
- package/lib/plugins/respArea/utils.js.map +0 -1
- package/lib/plugins/table/icons/index.js +0 -69
- package/lib/plugins/table/icons/index.js.map +0 -1
- package/lib/plugins/table/index.js +0 -499
- package/lib/plugins/table/index.js.map +0 -1
- package/lib/plugins/table/table-toolbar.js +0 -158
- package/lib/plugins/table/table-toolbar.js.map +0 -1
- package/lib/plugins/toolbar/default-toolbar.js +0 -174
- package/lib/plugins/toolbar/default-toolbar.js.map +0 -1
- package/lib/plugins/toolbar/done-button.js +0 -50
- package/lib/plugins/toolbar/done-button.js.map +0 -1
- package/lib/plugins/toolbar/editor-and-toolbar.js +0 -287
- package/lib/plugins/toolbar/editor-and-toolbar.js.map +0 -1
- package/lib/plugins/toolbar/index.js +0 -34
- package/lib/plugins/toolbar/index.js.map +0 -1
- package/lib/plugins/toolbar/toolbar-buttons.js +0 -161
- package/lib/plugins/toolbar/toolbar-buttons.js.map +0 -1
- package/lib/plugins/toolbar/toolbar.js +0 -352
- package/lib/plugins/toolbar/toolbar.js.map +0 -1
- package/lib/plugins/utils.js +0 -62
- package/lib/plugins/utils.js.map +0 -1
- package/lib/serialization.js +0 -488
- package/lib/serialization.js.map +0 -1
- package/lib/theme.js +0 -9
- package/lib/theme.js.map +0 -1
- package/playground/image/data.js +0 -59
- package/playground/image/index.html +0 -22
- package/playground/image/index.jsx +0 -81
- package/playground/index.html +0 -25
- package/playground/mathquill/index.html +0 -22
- package/playground/mathquill/index.jsx +0 -155
- package/playground/package.json +0 -15
- package/playground/prod-test/index.html +0 -22
- package/playground/prod-test/index.jsx +0 -28
- package/playground/schema-override/data.js +0 -29
- package/playground/schema-override/image-plugin.jsx +0 -41
- package/playground/schema-override/index.html +0 -21
- package/playground/schema-override/index.jsx +0 -97
- package/playground/serialization/data.js +0 -29
- package/playground/serialization/image-plugin.jsx +0 -41
- package/playground/serialization/index.html +0 -22
- package/playground/serialization/index.jsx +0 -12
- package/playground/static.json +0 -3
- package/playground/table-examples.html +0 -70
- package/playground/webpack.config.js +0 -42
- package/static.json +0 -1
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.CustomToolbarComp = void 0;
|
|
9
|
-
exports["default"] = MathPlugin;
|
|
10
|
-
exports.serialization = exports.inlineMath = void 0;
|
|
11
|
-
|
|
12
|
-
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
13
|
-
|
|
14
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
15
|
-
|
|
16
|
-
var _Functions = _interopRequireDefault(require("@material-ui/icons/Functions"));
|
|
17
|
-
|
|
18
|
-
var _slate = require("slate");
|
|
19
|
-
|
|
20
|
-
var _mathToolbar = require("@pie-lib/math-toolbar");
|
|
21
|
-
|
|
22
|
-
var _mathRendering = require("@pie-lib/math-rendering");
|
|
23
|
-
|
|
24
|
-
var _react = _interopRequireDefault(require("react"));
|
|
25
|
-
|
|
26
|
-
var _debug = _interopRequireDefault(require("debug"));
|
|
27
|
-
|
|
28
|
-
var _slatePropTypes = _interopRequireDefault(require("slate-prop-types"));
|
|
29
|
-
|
|
30
|
-
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
31
|
-
|
|
32
|
-
var _serialization = require("../../serialization");
|
|
33
|
-
|
|
34
|
-
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
|
|
35
|
-
|
|
36
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
37
|
-
|
|
38
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
39
|
-
|
|
40
|
-
var log = (0, _debug["default"])('@pie-lib:editable-html:plugins:math');
|
|
41
|
-
var TEXT_NODE = 3;
|
|
42
|
-
|
|
43
|
-
function generateAdditionalKeys() {
|
|
44
|
-
var keyData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
45
|
-
return keyData.map(function (key) {
|
|
46
|
-
return {
|
|
47
|
-
name: key,
|
|
48
|
-
latex: key,
|
|
49
|
-
write: key,
|
|
50
|
-
label: key
|
|
51
|
-
};
|
|
52
|
-
});
|
|
53
|
-
} // eslint-disable-next-line react/display-name
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
var CustomToolbarComp = /*#__PURE__*/_react["default"].memo(function (props) {
|
|
57
|
-
var node = props.node,
|
|
58
|
-
value = props.value,
|
|
59
|
-
onFocus = props.onFocus,
|
|
60
|
-
onBlur = props.onBlur,
|
|
61
|
-
onClick = props.onClick;
|
|
62
|
-
|
|
63
|
-
var _ref = props || {},
|
|
64
|
-
pluginProps = _ref.pluginProps;
|
|
65
|
-
|
|
66
|
-
var _ref2 = pluginProps || {},
|
|
67
|
-
math = _ref2.math;
|
|
68
|
-
|
|
69
|
-
var _ref3 = math || {},
|
|
70
|
-
keypadMode = _ref3.keypadMode,
|
|
71
|
-
customKeys = _ref3.customKeys,
|
|
72
|
-
_ref3$controlledKeypa = _ref3.controlledKeypadMode,
|
|
73
|
-
controlledKeypadMode = _ref3$controlledKeypa === void 0 ? true : _ref3$controlledKeypa;
|
|
74
|
-
|
|
75
|
-
var onDone = function onDone(latex) {
|
|
76
|
-
var update = _objectSpread(_objectSpread({}, node.data.toObject()), {}, {
|
|
77
|
-
latex: latex
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
var change = value.change().setNodeByKey(node.key, {
|
|
81
|
-
data: update
|
|
82
|
-
});
|
|
83
|
-
var nextText = value.document.getNextText(node.key);
|
|
84
|
-
change.moveFocusTo(nextText.key, 0).moveAnchorTo(nextText.key, 0);
|
|
85
|
-
props.onToolbarDone(change, false);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
var onChange = function onChange(latex) {
|
|
89
|
-
var update = _objectSpread(_objectSpread({}, node.data.toObject()), {}, {
|
|
90
|
-
latex: latex
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
var change = value.change().setNodeByKey(node.key, {
|
|
94
|
-
data: update
|
|
95
|
-
});
|
|
96
|
-
log('call onToolbarChange:', change);
|
|
97
|
-
props.onDataChange(node.key, update);
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
var latex = node.data.get('latex');
|
|
101
|
-
return /*#__PURE__*/_react["default"].createElement(_mathToolbar.MathToolbar, {
|
|
102
|
-
autoFocus: true,
|
|
103
|
-
additionalKeys: generateAdditionalKeys(customKeys),
|
|
104
|
-
latex: latex,
|
|
105
|
-
onChange: onChange,
|
|
106
|
-
onDone: onDone,
|
|
107
|
-
onBlur: onBlur,
|
|
108
|
-
onFocus: onFocus,
|
|
109
|
-
onClick: onClick,
|
|
110
|
-
keypadMode: keypadMode,
|
|
111
|
-
controlledKeypadMode: controlledKeypadMode
|
|
112
|
-
});
|
|
113
|
-
}, function (prev, next) {
|
|
114
|
-
var node = prev.node,
|
|
115
|
-
_prev$pluginProps = prev.pluginProps;
|
|
116
|
-
_prev$pluginProps = _prev$pluginProps === void 0 ? {} : _prev$pluginProps;
|
|
117
|
-
var _prev$pluginProps$mat = _prev$pluginProps.math;
|
|
118
|
-
_prev$pluginProps$mat = _prev$pluginProps$mat === void 0 ? {} : _prev$pluginProps$mat;
|
|
119
|
-
var keypadMode = _prev$pluginProps$mat.keypadMode,
|
|
120
|
-
controlledKeypadMode = _prev$pluginProps$mat.controlledKeypadMode;
|
|
121
|
-
var nodeNext = next.node,
|
|
122
|
-
_next$pluginProps = next.pluginProps;
|
|
123
|
-
_next$pluginProps = _next$pluginProps === void 0 ? {} : _next$pluginProps;
|
|
124
|
-
var _next$pluginProps$mat = _next$pluginProps.math;
|
|
125
|
-
_next$pluginProps$mat = _next$pluginProps$mat === void 0 ? {} : _next$pluginProps$mat;
|
|
126
|
-
var keypadModeNext = _next$pluginProps$mat.keypadMode,
|
|
127
|
-
controlledKeypadModeNext = _next$pluginProps$mat.controlledKeypadMode;
|
|
128
|
-
var keypadModeChanged = keypadMode !== keypadModeNext;
|
|
129
|
-
var controlledKeypadModeChanged = controlledKeypadMode !== controlledKeypadModeNext;
|
|
130
|
-
var equal = node.equals(nodeNext);
|
|
131
|
-
return equal && !keypadModeChanged && !controlledKeypadModeChanged;
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
exports.CustomToolbarComp = CustomToolbarComp;
|
|
135
|
-
CustomToolbarComp.propTypes = {
|
|
136
|
-
node: _slatePropTypes["default"].node.isRequired,
|
|
137
|
-
value: _slatePropTypes["default"].value,
|
|
138
|
-
onToolbarDone: _propTypes["default"].func,
|
|
139
|
-
onDataChange: _propTypes["default"].func,
|
|
140
|
-
onFocus: _propTypes["default"].func,
|
|
141
|
-
onClick: _propTypes["default"].func,
|
|
142
|
-
onBlur: _propTypes["default"].func
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
function MathPlugin(opts) {
|
|
146
|
-
MathPlugin.mathMlOptions = {
|
|
147
|
-
mmlOutput: opts.mmlOutput,
|
|
148
|
-
mmlEditing: opts.mmlEditing
|
|
149
|
-
};
|
|
150
|
-
return {
|
|
151
|
-
name: 'math',
|
|
152
|
-
toolbar: {
|
|
153
|
-
icon: /*#__PURE__*/_react["default"].createElement(_Functions["default"], null),
|
|
154
|
-
onClick: function onClick(value, onChange) {
|
|
155
|
-
log('[insertMath]');
|
|
156
|
-
var math = inlineMath();
|
|
157
|
-
var change = value.change().insertInline(math);
|
|
158
|
-
onChange(change);
|
|
159
|
-
},
|
|
160
|
-
supports: function supports(node) {
|
|
161
|
-
return node && node.object === 'inline' && node.type === 'math';
|
|
162
|
-
},
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Return a react component function
|
|
166
|
-
* @param node {Slate.Node}
|
|
167
|
-
* @param value {Slate.Value}
|
|
168
|
-
* @param onDone {(change?: Slate.Change, finishEditing :boolea) => void} - a function to call once the toolbar
|
|
169
|
-
* has made any changes, call with the node.key and a data object.
|
|
170
|
-
*/
|
|
171
|
-
CustomToolbarComp: CustomToolbarComp
|
|
172
|
-
},
|
|
173
|
-
schema: {
|
|
174
|
-
document: {
|
|
175
|
-
match: [{
|
|
176
|
-
type: 'math'
|
|
177
|
-
}]
|
|
178
|
-
}
|
|
179
|
-
},
|
|
180
|
-
pluginStyles: function pluginStyles(node, parentNode, p) {
|
|
181
|
-
if (p) {
|
|
182
|
-
return {
|
|
183
|
-
position: 'absolute',
|
|
184
|
-
top: 'initial'
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
},
|
|
188
|
-
renderNode: function renderNode(props) {
|
|
189
|
-
if (props.node.type === 'math') {
|
|
190
|
-
log('[renderNode]: data:', props.node.data);
|
|
191
|
-
return /*#__PURE__*/_react["default"].createElement(_mathToolbar.MathPreview, props);
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Here for rendering mathml content
|
|
195
|
-
*/
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (props.node.type === 'mathml') {
|
|
199
|
-
var html = props.node.data.get('html');
|
|
200
|
-
return /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({}, props.attributes, {
|
|
201
|
-
dangerouslySetInnerHTML: {
|
|
202
|
-
__html: html
|
|
203
|
-
}
|
|
204
|
-
}));
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
MathPlugin.ROUND_BRACKETS = 'round_brackets';
|
|
211
|
-
MathPlugin.SQUARE_BRACKETS = 'square_brackets';
|
|
212
|
-
MathPlugin.DOLLAR = 'dollar';
|
|
213
|
-
MathPlugin.DOUBLE_DOLLAR = 'double_dollar';
|
|
214
|
-
MathPlugin.mathMlOptions = {};
|
|
215
|
-
MathPlugin.propTypes = {
|
|
216
|
-
attributes: _propTypes["default"].object,
|
|
217
|
-
node: _slatePropTypes["default"].node
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
var inlineMath = function inlineMath() {
|
|
221
|
-
return _slate.Inline.create({
|
|
222
|
-
object: 'inline',
|
|
223
|
-
type: 'math',
|
|
224
|
-
isVoid: true,
|
|
225
|
-
data: {
|
|
226
|
-
latex: ''
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
exports.inlineMath = inlineMath;
|
|
232
|
-
|
|
233
|
-
var htmlDecode = function htmlDecode(input) {
|
|
234
|
-
var doc = new DOMParser().parseFromString(input, 'text/html');
|
|
235
|
-
return doc.documentElement.textContent;
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
var getTagName = function getTagName(el) {
|
|
239
|
-
return (el && el.tagName || '').toLowerCase();
|
|
240
|
-
};
|
|
241
|
-
/**
|
|
242
|
-
* Makes sure that strings that contain stuff like:
|
|
243
|
-
* x<y are not transformed into x by the DOMParser because it thinks
|
|
244
|
-
* that <y is the start of a dom element tag
|
|
245
|
-
* @param input
|
|
246
|
-
* @returns {*}
|
|
247
|
-
*/
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
var lessThanHandling = function lessThanHandling(input) {
|
|
251
|
-
var arrowSplit = input.split('<'); // if we don't have at least 2 characters there's no point in checking
|
|
252
|
-
|
|
253
|
-
if (input.length > 2) {
|
|
254
|
-
return arrowSplit.reduce(function (st, part) {
|
|
255
|
-
/*
|
|
256
|
-
We check if this element resulted is:
|
|
257
|
-
div - continuation of a beginning of a HTML element
|
|
258
|
-
/div - closing of a HTML tag
|
|
259
|
-
br/> - beginning and closing of a html TAG
|
|
260
|
-
*/
|
|
261
|
-
if (part.match(/<[a-zA-Z/][\s\S]*>/gi)) {
|
|
262
|
-
return "".concat(st).concat(st ? '<' : '').concat(part);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
return "".concat(st).concat(st ? '<' : '').concat(part);
|
|
266
|
-
}, '');
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
return input;
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
function fixLatexExpression(latexInput) {
|
|
273
|
-
// for some reason, mmlToLatex parses () incorrectly - or at least in a way that our interpreter can not use them
|
|
274
|
-
// Replace '\\left.' and '\\right.' with an empty string
|
|
275
|
-
return latexInput.replace(/\\left\.\s*|\\right\.\s*/g, '');
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
var serialization = {
|
|
279
|
-
deserialize: function deserialize(el) {
|
|
280
|
-
var tagName = getTagName(el);
|
|
281
|
-
/**
|
|
282
|
-
* This is used for when there's a wrapper over the mathml element.
|
|
283
|
-
* Because of this slate rule: "Only allow block nodes or inline and text nodes in blocks."
|
|
284
|
-
* The element that contains only the mathml is removed (along with the math) because it has
|
|
285
|
-
* an inline child and the block is of type block
|
|
286
|
-
* This is for legacy content only since our math rendering is valid for the core slate rules
|
|
287
|
-
*/
|
|
288
|
-
|
|
289
|
-
var hasMathChild = _serialization.BLOCK_TAGS[tagName] && el.childNodes.length === 1 && getTagName(el.firstChild) === 'math';
|
|
290
|
-
log('[deserialize] name: ', tagName);
|
|
291
|
-
/**
|
|
292
|
-
* This is here in order to be able to render mathml content
|
|
293
|
-
*/
|
|
294
|
-
|
|
295
|
-
if (tagName === 'math' || el.dataset && el.dataset.type === 'mathml' || hasMathChild) {
|
|
296
|
-
var newHtml = hasMathChild ? el.innerHTML : el.outerHTML;
|
|
297
|
-
|
|
298
|
-
if (MathPlugin.mathMlOptions.mmlEditing) {
|
|
299
|
-
// todo fix this in mathml-to-latex
|
|
300
|
-
var htmlWithRemovedSpaces = newHtml.replaceAll(" ", " ");
|
|
301
|
-
var htmlToUse = (0, _mathRendering.mmlToLatex)(htmlWithRemovedSpaces);
|
|
302
|
-
var latex = htmlDecode(htmlToUse); // todo fix this in mathml-to-latex
|
|
303
|
-
|
|
304
|
-
var correctedLatex = fixLatexExpression(latex);
|
|
305
|
-
|
|
306
|
-
var _unWrapMath = (0, _mathRendering.unWrapMath)(correctedLatex),
|
|
307
|
-
unwrapped = _unWrapMath.unwrapped,
|
|
308
|
-
wrapType = _unWrapMath.wrapType;
|
|
309
|
-
|
|
310
|
-
return {
|
|
311
|
-
object: 'inline',
|
|
312
|
-
type: 'math',
|
|
313
|
-
isVoid: true,
|
|
314
|
-
nodes: [],
|
|
315
|
-
data: {
|
|
316
|
-
latex: unwrapped,
|
|
317
|
-
wrapper: wrapType
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
return {
|
|
323
|
-
object: 'inline',
|
|
324
|
-
isVoid: true,
|
|
325
|
-
type: 'mathml',
|
|
326
|
-
data: {
|
|
327
|
-
html: newHtml
|
|
328
|
-
}
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
if (el.nodeType === TEXT_NODE) {
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
if (tagName !== 'span') {
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
var hasLatex = el.hasAttribute('data-latex') || el.hasAttribute('latex');
|
|
341
|
-
|
|
342
|
-
if (hasLatex) {
|
|
343
|
-
var _latex = htmlDecode(el.innerHTML);
|
|
344
|
-
|
|
345
|
-
var _unWrapMath2 = (0, _mathRendering.unWrapMath)(_latex),
|
|
346
|
-
_unwrapped = _unWrapMath2.unwrapped,
|
|
347
|
-
_wrapType = _unWrapMath2.wrapType;
|
|
348
|
-
|
|
349
|
-
log('[deserialize]: noBrackets: ', _unwrapped, _wrapType);
|
|
350
|
-
return {
|
|
351
|
-
object: 'inline',
|
|
352
|
-
type: 'math',
|
|
353
|
-
isVoid: true,
|
|
354
|
-
nodes: [],
|
|
355
|
-
data: {
|
|
356
|
-
latex: _unwrapped,
|
|
357
|
-
wrapper: _wrapType
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
},
|
|
362
|
-
serialize: function serialize(object) {
|
|
363
|
-
if (object.type === 'math') {
|
|
364
|
-
var l = object.data.get('latex');
|
|
365
|
-
var wrapper = object.data.get('wrapper');
|
|
366
|
-
log('[serialize] latex: ', l);
|
|
367
|
-
var decoded = htmlDecode(lessThanHandling(l));
|
|
368
|
-
|
|
369
|
-
if (MathPlugin.mathMlOptions.mmlOutput) {
|
|
370
|
-
var res = (0, _mathRendering.renderMath)("<span data-latex=\"\" data-raw=\"".concat(decoded, "\">").concat((0, _mathRendering.wrapMath)(decoded, wrapper), "</span>"));
|
|
371
|
-
var newLatex = (0, _mathRendering.mmlToLatex)(res); // we need to remove all the spaces from the latex to be able to compare it
|
|
372
|
-
|
|
373
|
-
var strippedL = l.replace(/\s/g, '');
|
|
374
|
-
var strippedNewL = newLatex.replace(/\s/g, ''); // we check if the latex keeps his form after being converted to mathml and back to latex
|
|
375
|
-
// if it does we can safely convert it to mathml
|
|
376
|
-
|
|
377
|
-
if (!(0, _isEqual["default"])(strippedL, strippedNewL)) {
|
|
378
|
-
var correctedLatex = fixLatexExpression(newLatex); // As George requested in PD-3167, I will set the new mathML anyway, and also log differences
|
|
379
|
-
// if it doesn't we keep the latex version
|
|
380
|
-
|
|
381
|
-
console.log('This latex can not be safely converted to mathml so we will keep the latex version!!!', {
|
|
382
|
-
initialLatex: l,
|
|
383
|
-
newLatex: newLatex,
|
|
384
|
-
correctedLatex: correctedLatex,
|
|
385
|
-
mathML: res
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
return /*#__PURE__*/_react["default"].createElement("span", {
|
|
390
|
-
"data-type": "mathml",
|
|
391
|
-
dangerouslySetInnerHTML: {
|
|
392
|
-
__html: res
|
|
393
|
-
}
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
return /*#__PURE__*/_react["default"].createElement("span", {
|
|
398
|
-
"data-latex": "",
|
|
399
|
-
"data-raw": decoded
|
|
400
|
-
}, (0, _mathRendering.wrapMath)(decoded, wrapper));
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Here for rendering mathml content
|
|
404
|
-
*/
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
if (object.type === 'mathml') {
|
|
408
|
-
var html = object.data.get('html');
|
|
409
|
-
return /*#__PURE__*/_react["default"].createElement("span", {
|
|
410
|
-
"data-type": "mathml",
|
|
411
|
-
dangerouslySetInnerHTML: {
|
|
412
|
-
__html: html
|
|
413
|
-
}
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
};
|
|
418
|
-
exports.serialization = serialization;
|
|
419
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/plugins/math/index.jsx"],"names":["log","TEXT_NODE","generateAdditionalKeys","keyData","map","key","name","latex","write","label","CustomToolbarComp","React","memo","props","node","value","onFocus","onBlur","onClick","pluginProps","math","keypadMode","customKeys","controlledKeypadMode","onDone","update","data","toObject","change","setNodeByKey","nextText","document","getNextText","moveFocusTo","moveAnchorTo","onToolbarDone","onChange","onDataChange","get","prev","next","nodeNext","keypadModeNext","controlledKeypadModeNext","keypadModeChanged","controlledKeypadModeChanged","equal","equals","propTypes","SlatePropTypes","isRequired","PropTypes","func","MathPlugin","opts","mathMlOptions","mmlOutput","mmlEditing","toolbar","icon","inlineMath","insertInline","supports","object","type","schema","match","pluginStyles","parentNode","p","position","top","renderNode","html","attributes","__html","ROUND_BRACKETS","SQUARE_BRACKETS","DOLLAR","DOUBLE_DOLLAR","Inline","create","isVoid","htmlDecode","input","doc","DOMParser","parseFromString","documentElement","textContent","getTagName","el","tagName","toLowerCase","lessThanHandling","arrowSplit","split","length","reduce","st","part","fixLatexExpression","latexInput","replace","serialization","deserialize","hasMathChild","BLOCK_TAGS","childNodes","firstChild","dataset","newHtml","innerHTML","outerHTML","htmlWithRemovedSpaces","replaceAll","htmlToUse","correctedLatex","unwrapped","wrapType","nodes","wrapper","nodeType","hasLatex","hasAttribute","serialize","l","decoded","res","newLatex","strippedL","strippedNewL","console","initialLatex","mathML"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AAEA;;AACA;;;;;;AAEA,IAAMA,GAAG,GAAG,uBAAM,qCAAN,CAAZ;AAEA,IAAMC,SAAS,GAAG,CAAlB;;AAEA,SAASC,sBAAT,GAA8C;AAAA,MAAdC,OAAc,uEAAJ,EAAI;AAC5C,SAAOA,OAAO,CAACC,GAAR,CAAY,UAACC,GAAD;AAAA,WAAU;AAC3BC,MAAAA,IAAI,EAAED,GADqB;AAE3BE,MAAAA,KAAK,EAAEF,GAFoB;AAG3BG,MAAAA,KAAK,EAAEH,GAHoB;AAI3BI,MAAAA,KAAK,EAAEJ;AAJoB,KAAV;AAAA,GAAZ,CAAP;AAMD,C,CAED;;;AACO,IAAMK,iBAAiB,gBAAGC,kBAAMC,IAAN,CAC/B,UAACC,KAAD,EAAW;AACT,MAAQC,IAA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CAACpI,GAAR,CAAY,uFAAZ,EAAqG;AACnGqI,YAAAA,YAAY,EAAEP,CADqF;AAEnGG,YAAAA,QAAQ,EAAEA,QAFyF;AAGnGZ,YAAAA,cAAc,EAAdA,cAHmG;AAInGiB,YAAAA,MAAM,EAAEN;AAJ2F,WAArG;AAMD;;AAED,4BAAO;AAAM,uBAAU,QAAhB;AAAyB,UAAA,uBAAuB,EAAE;AAAErD,YAAAA,MAAM,EAAEqD;AAAV;AAAlD,UAAP;AACD;;AAED,0BACE;AAAM,sBAAW,EAAjB;AAAoB,oBAAUD;AAA9B,SACG,6BAASA,OAAT,EAAkBN,OAAlB,CADH,CADF;AAKD;AAED;AACJ;AACA;;;AACI,QAAI1D,MAAM,CAACC,IAAP,KAAgB,QAApB,EAA8B;AAC5B,UAAMS,IAAI,GAAGV,MAAM,CAACrC,IAAP,CAAYY,GAAZ,CAAgB,MAAhB,CAAb;AAEA,0BAAO;AAAM,qBAAU,QAAhB;AAAyB,QAAA,uBAAuB,EAAE;AAAEqC,UAAAA,MAAM,EAAEF;AAAV;AAAlD,QAAP;AACD;AACF;AA5H0B,CAAtB","sourcesContent":["import Functions from '@material-ui/icons/Functions';\nimport { Inline } from 'slate';\nimport { MathPreview, MathToolbar } from '@pie-lib/math-toolbar';\nimport { wrapMath, unWrapMath, mmlToLatex, renderMath } from '@pie-lib/math-rendering';\nimport React from 'react';\nimport debug from 'debug';\nimport SlatePropTypes from 'slate-prop-types';\nimport PropTypes from 'prop-types';\n\nimport { BLOCK_TAGS } from '../../serialization';\nimport isEqual from 'lodash/isEqual';\n\nconst log = debug('@pie-lib:editable-html:plugins:math');\n\nconst TEXT_NODE = 3;\n\nfunction generateAdditionalKeys(keyData = []) {\n return keyData.map((key) => ({\n name: key,\n latex: key,\n write: key,\n label: key,\n }));\n}\n\n// eslint-disable-next-line react/display-name\nexport const CustomToolbarComp = React.memo(\n (props) => {\n const { node, value, onFocus, onBlur, onClick } = props;\n const { pluginProps } = props || {};\n const { math } = pluginProps || {};\n const { keypadMode, customKeys, controlledKeypadMode = true } = math || {};\n\n const onDone = (latex) => {\n const update = {\n ...node.data.toObject(),\n latex,\n };\n const change = value.change().setNodeByKey(node.key, { data: update });\n\n const nextText = value.document.getNextText(node.key);\n\n change.moveFocusTo(nextText.key, 0).moveAnchorTo(nextText.key, 0);\n\n props.onToolbarDone(change, false);\n };\n\n const onChange = (latex) => {\n const update = {\n ...node.data.toObject(),\n latex,\n };\n const change = value.change().setNodeByKey(node.key, { data: update });\n log('call onToolbarChange:', change);\n props.onDataChange(node.key, update);\n };\n\n const latex = node.data.get('latex');\n\n return (\n <MathToolbar\n autoFocus\n additionalKeys={generateAdditionalKeys(customKeys)}\n latex={latex}\n onChange={onChange}\n onDone={onDone}\n onBlur={onBlur}\n onFocus={onFocus}\n onClick={onClick}\n keypadMode={keypadMode}\n controlledKeypadMode={controlledKeypadMode}\n />\n );\n },\n (prev, next) => {\n const { node, pluginProps: { math: { keypadMode, controlledKeypadMode } = {} } = {} } = prev;\n const {\n node: nodeNext,\n pluginProps: { math: { keypadMode: keypadModeNext, controlledKeypadMode: controlledKeypadModeNext } = {} } = {},\n } = next;\n const keypadModeChanged = keypadMode !== keypadModeNext;\n const controlledKeypadModeChanged = controlledKeypadMode !== controlledKeypadModeNext;\n\n const equal = node.equals(nodeNext);\n return equal && !keypadModeChanged && !controlledKeypadModeChanged;\n },\n);\n\nCustomToolbarComp.propTypes = {\n node: SlatePropTypes.node.isRequired,\n value: SlatePropTypes.value,\n onToolbarDone: PropTypes.func,\n onDataChange: PropTypes.func,\n onFocus: PropTypes.func,\n onClick: PropTypes.func,\n onBlur: PropTypes.func,\n};\n\nexport default function MathPlugin(opts) {\n MathPlugin.mathMlOptions = {\n mmlOutput: opts.mmlOutput,\n mmlEditing: opts.mmlEditing,\n };\n\n return {\n name: 'math',\n toolbar: {\n icon: <Functions />,\n onClick: (value, onChange) => {\n log('[insertMath]');\n const math = inlineMath();\n const change = value.change().insertInline(math);\n onChange(change);\n },\n supports: (node) => node && node.object === 'inline' && node.type === 'math',\n /**\n * Return a react component function\n * @param node {Slate.Node}\n * @param value {Slate.Value}\n * @param onDone {(change?: Slate.Change, finishEditing :boolea) => void} - a function to call once the toolbar\n * has made any changes, call with the node.key and a data object.\n */\n CustomToolbarComp,\n },\n schema: {\n document: { match: [{ type: 'math' }] },\n },\n\n pluginStyles: (node, parentNode, p) => {\n if (p) {\n return {\n position: 'absolute',\n top: 'initial',\n };\n }\n },\n\n renderNode: (props) => {\n if (props.node.type === 'math') {\n log('[renderNode]: data:', props.node.data);\n return <MathPreview {...props} />;\n }\n\n /**\n * Here for rendering mathml content\n */\n if (props.node.type === 'mathml') {\n const html = props.node.data.get('html');\n\n return <span {...props.attributes} dangerouslySetInnerHTML={{ __html: html }} />;\n }\n },\n };\n}\n\nMathPlugin.ROUND_BRACKETS = 'round_brackets';\nMathPlugin.SQUARE_BRACKETS = 'square_brackets';\nMathPlugin.DOLLAR = 'dollar';\nMathPlugin.DOUBLE_DOLLAR = 'double_dollar';\nMathPlugin.mathMlOptions = {};\n\nMathPlugin.propTypes = {\n attributes: PropTypes.object,\n node: SlatePropTypes.node,\n};\n\nexport const inlineMath = () =>\n Inline.create({\n object: 'inline',\n type: 'math',\n isVoid: true,\n data: {\n latex: '',\n },\n });\n\nconst htmlDecode = (input) => {\n const doc = new DOMParser().parseFromString(input, 'text/html');\n\n return doc.documentElement.textContent;\n};\n\nconst getTagName = (el) => {\n return ((el && el.tagName) || '').toLowerCase();\n};\n\n/**\n * Makes sure that strings that contain stuff like:\n * x<y are not transformed into x by the DOMParser because it thinks\n * that <y is the start of a dom element tag\n * @param input\n * @returns {*}\n */\nconst lessThanHandling = (input) => {\n const arrowSplit = input.split('<');\n\n // if we don't have at least 2 characters there's no point in checking\n if (input.length > 2) {\n return arrowSplit.reduce((st, part) => {\n /*\n We check if this element resulted is:\n div - continuation of a beginning of a HTML element\n /div - closing of a HTML tag\n br/> - beginning and closing of a html TAG\n */\n if (part.match(/<[a-zA-Z/][\\s\\S]*>/gi)) {\n return `${st}${st ? '<' : ''}${part}`;\n }\n\n return `${st}${st ? '<' : ''}${part}`;\n }, '');\n }\n\n return input;\n};\n\nfunction fixLatexExpression(latexInput) {\n // for some reason, mmlToLatex parses () incorrectly - or at least in a way that our interpreter can not use them\n // Replace '\\\\left.' and '\\\\right.' with an empty string\n return latexInput.replace(/\\\\left\\.\\s*|\\\\right\\.\\s*/g, '');\n}\n\nexport const serialization = {\n deserialize(el) {\n const tagName = getTagName(el);\n /**\n * This is used for when there's a wrapper over the mathml element.\n * Because of this slate rule: \"Only allow block nodes or inline and text nodes in blocks.\"\n * The element that contains only the mathml is removed (along with the math) because it has\n * an inline child and the block is of type block\n * This is for legacy content only since our math rendering is valid for the core slate rules\n */\n const hasMathChild = BLOCK_TAGS[tagName] && el.childNodes.length === 1 && getTagName(el.firstChild) === 'math';\n log('[deserialize] name: ', tagName);\n\n /**\n * This is here in order to be able to render mathml content\n */\n if (tagName === 'math' || (el.dataset && el.dataset.type === 'mathml') || hasMathChild) {\n const newHtml = hasMathChild ? el.innerHTML : el.outerHTML;\n\n if (MathPlugin.mathMlOptions.mmlEditing) {\n // todo fix this in mathml-to-latex\n const htmlWithRemovedSpaces = newHtml.replaceAll(\" \", \" \");\n const htmlToUse = mmlToLatex(htmlWithRemovedSpaces);\n const latex = htmlDecode(htmlToUse);\n // todo fix this in mathml-to-latex\n const correctedLatex = fixLatexExpression(latex);\n const { unwrapped, wrapType } = unWrapMath(correctedLatex);\n\n return {\n object: 'inline',\n type: 'math',\n isVoid: true,\n nodes: [],\n data: {\n latex: unwrapped,\n wrapper: wrapType,\n },\n };\n }\n\n return {\n object: 'inline',\n isVoid: true,\n type: 'mathml',\n data: {\n html: newHtml,\n },\n };\n }\n\n if (el.nodeType === TEXT_NODE) {\n return;\n }\n\n if (tagName !== 'span') {\n return;\n }\n\n const hasLatex = el.hasAttribute('data-latex') || el.hasAttribute('latex');\n\n if (hasLatex) {\n const latex = htmlDecode(el.innerHTML);\n const { unwrapped, wrapType } = unWrapMath(latex);\n log('[deserialize]: noBrackets: ', unwrapped, wrapType);\n return {\n object: 'inline',\n type: 'math',\n isVoid: true,\n nodes: [],\n data: {\n latex: unwrapped,\n wrapper: wrapType,\n },\n };\n }\n },\n serialize(object) {\n if (object.type === 'math') {\n const l = object.data.get('latex');\n const wrapper = object.data.get('wrapper');\n log('[serialize] latex: ', l);\n const decoded = htmlDecode(lessThanHandling(l));\n\n if (MathPlugin.mathMlOptions.mmlOutput) {\n const res = renderMath(`<span data-latex=\"\" data-raw=\"${decoded}\">${wrapMath(decoded, wrapper)}</span>`);\n const newLatex = mmlToLatex(res);\n\n // we need to remove all the spaces from the latex to be able to compare it\n const strippedL = l.replace(/\\s/g, '');\n const strippedNewL = newLatex.replace(/\\s/g, '');\n\n // we check if the latex keeps his form after being converted to mathml and back to latex\n // if it does we can safely convert it to mathml\n if (!isEqual(strippedL, strippedNewL)) {\n const correctedLatex = fixLatexExpression(newLatex);\n\n // As George requested in PD-3167, I will set the new mathML anyway, and also log differences\n // if it doesn't we keep the latex version\n console.log('This latex can not be safely converted to mathml so we will keep the latex version!!!', {\n initialLatex: l,\n newLatex: newLatex,\n correctedLatex,\n mathML: res,\n });\n }\n\n return <span data-type=\"mathml\" dangerouslySetInnerHTML={{ __html: res }} />;\n }\n\n return (\n <span data-latex=\"\" data-raw={decoded}>\n {wrapMath(decoded, wrapper)}\n </span>\n );\n }\n\n /**\n * Here for rendering mathml content\n */\n if (object.type === 'mathml') {\n const html = object.data.get('html');\n\n return <span data-type=\"mathml\" dangerouslySetInnerHTML={{ __html: html }} />;\n }\n },\n};\n"],"file":"index.js"}
|