react-headless-mde 0.0.7 → 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.
- package/.github/workflows/npm-publish-next.yml +26 -0
- package/.github/workflows/npm-publish.yml +10 -8
- package/{readme.md → README.md} +3 -0
- package/dist/cjs/index.js +92 -96
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +92 -96
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.ts +5 -8
- package/package.json +4 -6
- package/rollup.config.js +1 -1
- package/.prettierrc +0 -8
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
+
# For more information see: https://docs.github.com/ru/actions/quickstart
|
|
3
|
+
|
|
4
|
+
name: Publish with @next
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches:
|
|
9
|
+
- 'develop'
|
|
10
|
+
# release:
|
|
11
|
+
# types: [created]
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
build-and-publish:
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v3
|
|
18
|
+
- uses: actions/setup-node@v3
|
|
19
|
+
with:
|
|
20
|
+
node-version: 18
|
|
21
|
+
registry-url: 'https://registry.npmjs.org'
|
|
22
|
+
- run: npm install
|
|
23
|
+
- run: npm run build
|
|
24
|
+
- run: npm publish --tag next
|
|
25
|
+
env:
|
|
26
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
-
# For more information see: https://
|
|
2
|
+
# For more information see: https://docs.github.com/ru/actions/quickstart
|
|
3
3
|
|
|
4
|
-
name: Publish
|
|
4
|
+
name: Publish
|
|
5
5
|
|
|
6
6
|
on:
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
push:
|
|
8
|
+
branches:
|
|
9
|
+
- 'main'
|
|
10
|
+
# release:
|
|
11
|
+
# types: [created]
|
|
9
12
|
|
|
10
13
|
jobs:
|
|
11
14
|
build-and-publish:
|
|
@@ -14,11 +17,10 @@ jobs:
|
|
|
14
17
|
- uses: actions/checkout@v3
|
|
15
18
|
- uses: actions/setup-node@v3
|
|
16
19
|
with:
|
|
17
|
-
node-version:
|
|
20
|
+
node-version: 18
|
|
18
21
|
registry-url: 'https://registry.npmjs.org'
|
|
19
|
-
- run:
|
|
20
|
-
- run:
|
|
22
|
+
- run: npm install
|
|
23
|
+
- run: npm run build
|
|
21
24
|
- run: npm publish
|
|
22
|
-
# - run: npm publish --tag next
|
|
23
25
|
env:
|
|
24
26
|
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/{readme.md → README.md}
RENAMED
|
@@ -45,6 +45,9 @@ export const MarkdownEditor = () => {
|
|
|
45
45
|
```
|
|
46
46
|
## Todo
|
|
47
47
|
|
|
48
|
+
### Add to awesome-react-headless-components on stable version
|
|
49
|
+
https://github.com/jxom/awesome-react-headless-components
|
|
50
|
+
|
|
48
51
|
### Third party
|
|
49
52
|
- https://github.com/grassator/insert-text-at-cursor by https://twitter.com/d_kubyshkin
|
|
50
53
|
|
package/dist/cjs/index.js
CHANGED
|
@@ -11,7 +11,7 @@ var react = require('react');
|
|
|
11
11
|
function getSurroundingWord(text, position) {
|
|
12
12
|
if (!text)
|
|
13
13
|
throw Error("Argument 'text' should be truthy");
|
|
14
|
-
var isWordDelimiter = function (c) { return c ===
|
|
14
|
+
var isWordDelimiter = function (c) { return c === ' ' || c.charCodeAt(0) === 10; };
|
|
15
15
|
// leftIndex is initialized to 0 because if selection is 0, it won't even enter the iteration
|
|
16
16
|
var start = 0;
|
|
17
17
|
// rightIndex is initialized to text.length because if selection is equal to text.length it won't even enter the interation
|
|
@@ -51,7 +51,7 @@ function selectWord(_a) {
|
|
|
51
51
|
* to make sure there's an empty line between 'startPosition' and the previous text
|
|
52
52
|
*/
|
|
53
53
|
function getBreaksNeededForEmptyLineBefore(text, startPosition) {
|
|
54
|
-
if (text === void 0) { text =
|
|
54
|
+
if (text === void 0) { text = ''; }
|
|
55
55
|
if (startPosition === 0)
|
|
56
56
|
return 0;
|
|
57
57
|
// rules:
|
|
@@ -79,7 +79,7 @@ function getBreaksNeededForEmptyLineBefore(text, startPosition) {
|
|
|
79
79
|
* to make sure there's an empty line between 'startPosition' and the next text
|
|
80
80
|
*/
|
|
81
81
|
function getBreaksNeededForEmptyLineAfter(text, startPosition) {
|
|
82
|
-
if (text === void 0) { text =
|
|
82
|
+
if (text === void 0) { text = ''; }
|
|
83
83
|
if (startPosition === text.length - 1)
|
|
84
84
|
return 0;
|
|
85
85
|
// rules:
|
|
@@ -120,18 +120,18 @@ function insertBeforeEachLine(selectedText, insertBefore) {
|
|
|
120
120
|
var insertionLength = 0;
|
|
121
121
|
var modifiedText = lines
|
|
122
122
|
.map(function (item, index) {
|
|
123
|
-
if (typeof insertBefore ===
|
|
123
|
+
if (typeof insertBefore === 'string') {
|
|
124
124
|
insertionLength += insertBefore.length;
|
|
125
125
|
return insertBefore + item;
|
|
126
126
|
}
|
|
127
|
-
else if (typeof insertBefore ===
|
|
127
|
+
else if (typeof insertBefore === 'function') {
|
|
128
128
|
var insertionResult = insertBefore(item, index);
|
|
129
129
|
insertionLength += insertionResult.length;
|
|
130
130
|
return insertBefore(item, index) + item;
|
|
131
131
|
}
|
|
132
|
-
throw Error(
|
|
132
|
+
throw Error('insertion is expected to be either a string or a function');
|
|
133
133
|
})
|
|
134
|
-
.join(
|
|
134
|
+
.join('\n');
|
|
135
135
|
return { modifiedText: modifiedText, insertionLength: insertionLength };
|
|
136
136
|
}
|
|
137
137
|
|
|
@@ -151,7 +151,7 @@ function setHeader(initialState, api, prefix) {
|
|
|
151
151
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
152
152
|
var newSelectionRange = selectWord({
|
|
153
153
|
text: initialState.text,
|
|
154
|
-
selection: initialState.selection
|
|
154
|
+
selection: initialState.selection,
|
|
155
155
|
});
|
|
156
156
|
var state1 = api.setSelectionRange(newSelectionRange);
|
|
157
157
|
// Add the prefix to the selection
|
|
@@ -159,7 +159,7 @@ function setHeader(initialState, api, prefix) {
|
|
|
159
159
|
// Adjust the selection to not contain the prefix
|
|
160
160
|
api.setSelectionRange({
|
|
161
161
|
start: state2.selection.end - getSelectedText(state1).length,
|
|
162
|
-
end: state2.selection.end
|
|
162
|
+
end: state2.selection.end,
|
|
163
163
|
});
|
|
164
164
|
}
|
|
165
165
|
|
|
@@ -171,21 +171,21 @@ var headerHelpers = /*#__PURE__*/Object.freeze({
|
|
|
171
171
|
var headingLevel1Command = {
|
|
172
172
|
execute: function (_a) {
|
|
173
173
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
174
|
-
setHeader(initialState, textApi,
|
|
175
|
-
}
|
|
174
|
+
setHeader(initialState, textApi, '# ');
|
|
175
|
+
},
|
|
176
176
|
};
|
|
177
177
|
|
|
178
178
|
var boldCommand = {
|
|
179
179
|
shouldUndo: function (options) {
|
|
180
|
-
return (getCharactersBeforeSelection(options.initialState, 2) ===
|
|
181
|
-
getCharactersAfterSelection(options.initialState, 2) ===
|
|
180
|
+
return (getCharactersBeforeSelection(options.initialState, 2) === '**' &&
|
|
181
|
+
getCharactersAfterSelection(options.initialState, 2) === '**');
|
|
182
182
|
},
|
|
183
183
|
execute: function (_a) {
|
|
184
184
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
185
185
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
186
186
|
var newSelectionRange = selectWord({
|
|
187
187
|
text: initialState.text,
|
|
188
|
-
selection: initialState.selection
|
|
188
|
+
selection: initialState.selection,
|
|
189
189
|
});
|
|
190
190
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
191
191
|
// Replaces the current selection with the bold mark up
|
|
@@ -193,7 +193,7 @@ var boldCommand = {
|
|
|
193
193
|
// Adjust the selection to not contain the **
|
|
194
194
|
textApi.setSelectionRange({
|
|
195
195
|
start: state2.selection.end - 2 - getSelectedText(state1).length,
|
|
196
|
-
end: state2.selection.end - 2
|
|
196
|
+
end: state2.selection.end - 2,
|
|
197
197
|
});
|
|
198
198
|
},
|
|
199
199
|
undo: function (_a) {
|
|
@@ -201,27 +201,27 @@ var boldCommand = {
|
|
|
201
201
|
var text = getSelectedText(initialState);
|
|
202
202
|
textApi.setSelectionRange({
|
|
203
203
|
start: initialState.selection.start - 2,
|
|
204
|
-
end: initialState.selection.end + 2
|
|
204
|
+
end: initialState.selection.end + 2,
|
|
205
205
|
});
|
|
206
206
|
textApi.replaceSelection(text);
|
|
207
207
|
textApi.setSelectionRange({
|
|
208
208
|
start: initialState.selection.start - 2,
|
|
209
|
-
end: initialState.selection.end - 2
|
|
209
|
+
end: initialState.selection.end - 2,
|
|
210
210
|
});
|
|
211
|
-
}
|
|
211
|
+
},
|
|
212
212
|
};
|
|
213
213
|
|
|
214
214
|
var italicCommand = {
|
|
215
215
|
shouldUndo: function (options) {
|
|
216
|
-
return (getCharactersBeforeSelection(options.initialState, 1) ===
|
|
217
|
-
getCharactersAfterSelection(options.initialState, 1) ===
|
|
216
|
+
return (getCharactersBeforeSelection(options.initialState, 1) === '*' &&
|
|
217
|
+
getCharactersAfterSelection(options.initialState, 1) === '*');
|
|
218
218
|
},
|
|
219
219
|
execute: function (_a) {
|
|
220
220
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
221
221
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
222
222
|
var newSelectionRange = selectWord({
|
|
223
223
|
text: initialState.text,
|
|
224
|
-
selection: initialState.selection
|
|
224
|
+
selection: initialState.selection,
|
|
225
225
|
});
|
|
226
226
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
227
227
|
// Replaces the current selection with the italic mark up
|
|
@@ -229,7 +229,7 @@ var italicCommand = {
|
|
|
229
229
|
// Adjust the selection to not contain the *
|
|
230
230
|
textApi.setSelectionRange({
|
|
231
231
|
start: state2.selection.end - 1 - getSelectedText(state1).length,
|
|
232
|
-
end: state2.selection.end - 1
|
|
232
|
+
end: state2.selection.end - 1,
|
|
233
233
|
});
|
|
234
234
|
},
|
|
235
235
|
undo: function (_a) {
|
|
@@ -237,14 +237,14 @@ var italicCommand = {
|
|
|
237
237
|
var text = getSelectedText(initialState);
|
|
238
238
|
textApi.setSelectionRange({
|
|
239
239
|
start: initialState.selection.start - 1,
|
|
240
|
-
end: initialState.selection.end + 1
|
|
240
|
+
end: initialState.selection.end + 1,
|
|
241
241
|
});
|
|
242
242
|
textApi.replaceSelection(text);
|
|
243
243
|
textApi.setSelectionRange({
|
|
244
244
|
start: initialState.selection.start - 1,
|
|
245
|
-
end: initialState.selection.end - 1
|
|
245
|
+
end: initialState.selection.end - 1,
|
|
246
246
|
});
|
|
247
|
-
}
|
|
247
|
+
},
|
|
248
248
|
};
|
|
249
249
|
|
|
250
250
|
var strikethroughCommand = {
|
|
@@ -253,7 +253,7 @@ var strikethroughCommand = {
|
|
|
253
253
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
254
254
|
var newSelectionRange = selectWord({
|
|
255
255
|
text: initialState.text,
|
|
256
|
-
selection: initialState.selection
|
|
256
|
+
selection: initialState.selection,
|
|
257
257
|
});
|
|
258
258
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
259
259
|
// Replaces the current selection with the strikethrough mark up
|
|
@@ -261,9 +261,9 @@ var strikethroughCommand = {
|
|
|
261
261
|
// Adjust the selection to not contain the ~~
|
|
262
262
|
textApi.setSelectionRange({
|
|
263
263
|
start: state2.selection.end - 2 - getSelectedText(state1).length,
|
|
264
|
-
end: state2.selection.end - 2
|
|
264
|
+
end: state2.selection.end - 2,
|
|
265
265
|
});
|
|
266
|
-
}
|
|
266
|
+
},
|
|
267
267
|
};
|
|
268
268
|
|
|
269
269
|
var linkCommand = {
|
|
@@ -272,7 +272,7 @@ var linkCommand = {
|
|
|
272
272
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
273
273
|
var newSelectionRange = selectWord({
|
|
274
274
|
text: initialState.text,
|
|
275
|
-
selection: initialState.selection
|
|
275
|
+
selection: initialState.selection,
|
|
276
276
|
});
|
|
277
277
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
278
278
|
// Replaces the current selection with the bold mark up
|
|
@@ -280,9 +280,9 @@ var linkCommand = {
|
|
|
280
280
|
// Adjust the selection to not contain the **
|
|
281
281
|
textApi.setSelectionRange({
|
|
282
282
|
start: state2.selection.end - 6 - getSelectedText(state1).length,
|
|
283
|
-
end: state2.selection.end - 6
|
|
283
|
+
end: state2.selection.end - 6,
|
|
284
284
|
});
|
|
285
|
-
}
|
|
285
|
+
},
|
|
286
286
|
};
|
|
287
287
|
|
|
288
288
|
var quoteCommand = {
|
|
@@ -291,22 +291,22 @@ var quoteCommand = {
|
|
|
291
291
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
292
292
|
var newSelectionRange = selectWord({
|
|
293
293
|
text: initialState.text,
|
|
294
|
-
selection: initialState.selection
|
|
294
|
+
selection: initialState.selection,
|
|
295
295
|
});
|
|
296
296
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
297
297
|
var breaksBeforeCount = getBreaksNeededForEmptyLineBefore(state1.text, state1.selection.start);
|
|
298
|
-
var breaksBefore = Array(breaksBeforeCount + 1).join(
|
|
298
|
+
var breaksBefore = Array(breaksBeforeCount + 1).join('\n');
|
|
299
299
|
var breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);
|
|
300
|
-
var breaksAfter = Array(breaksAfterCount + 1).join(
|
|
300
|
+
var breaksAfter = Array(breaksAfterCount + 1).join('\n');
|
|
301
301
|
// Replaces the current selection with the quote mark up
|
|
302
302
|
textApi.replaceSelection("".concat(breaksBefore, "> ").concat(getSelectedText(state1)).concat(breaksAfter));
|
|
303
303
|
var selectionStart = state1.selection.start + breaksBeforeCount + 2;
|
|
304
304
|
var selectionEnd = selectionStart + getSelectedText(state1).length;
|
|
305
305
|
textApi.setSelectionRange({
|
|
306
306
|
start: selectionStart,
|
|
307
|
-
end: selectionEnd
|
|
307
|
+
end: selectionEnd,
|
|
308
308
|
});
|
|
309
|
-
}
|
|
309
|
+
},
|
|
310
310
|
};
|
|
311
311
|
|
|
312
312
|
var imageCommand = {
|
|
@@ -315,17 +315,17 @@ var imageCommand = {
|
|
|
315
315
|
// Replaces the current selection with the whole word selected
|
|
316
316
|
var state1 = textApi.setSelectionRange(selectWord({
|
|
317
317
|
text: initialState.text,
|
|
318
|
-
selection: initialState.selection
|
|
318
|
+
selection: initialState.selection,
|
|
319
319
|
}));
|
|
320
320
|
// Replaces the current selection with the image
|
|
321
|
-
var imageTemplate = getSelectedText(state1) ||
|
|
321
|
+
var imageTemplate = getSelectedText(state1) || 'https://example.com/your-image.png';
|
|
322
322
|
textApi.replaceSelection(""));
|
|
323
323
|
// Adjust the selection to not contain the **
|
|
324
324
|
textApi.setSelectionRange({
|
|
325
325
|
start: state1.selection.start + 4,
|
|
326
|
-
end: state1.selection.start + 4 + imageTemplate.length
|
|
326
|
+
end: state1.selection.start + 4 + imageTemplate.length,
|
|
327
327
|
});
|
|
328
|
-
}
|
|
328
|
+
},
|
|
329
329
|
};
|
|
330
330
|
|
|
331
331
|
/******************************************************************************
|
|
@@ -359,7 +359,7 @@ function __generator(thisArg, body) {
|
|
|
359
359
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
360
360
|
function step(op) {
|
|
361
361
|
if (f) throw new TypeError("Generator is already executing.");
|
|
362
|
-
while (_) try {
|
|
362
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
363
363
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
364
364
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
365
365
|
switch (op[0]) {
|
|
@@ -383,9 +383,7 @@ function __generator(thisArg, body) {
|
|
|
383
383
|
|
|
384
384
|
var CommandController = /** @class */ (function () {
|
|
385
385
|
function CommandController(textController, commandMap) {
|
|
386
|
-
|
|
387
|
-
* Indicates whether there is a command currently executing
|
|
388
|
-
*/
|
|
386
|
+
// Indicates whether there is a command currently executing
|
|
389
387
|
this.isExecuting = false;
|
|
390
388
|
this.textController = textController;
|
|
391
389
|
this.commandMap = commandMap;
|
|
@@ -399,8 +397,8 @@ var CommandController = /** @class */ (function () {
|
|
|
399
397
|
case 0:
|
|
400
398
|
if (this.isExecuting) {
|
|
401
399
|
// The simplest thing to do is to ignore commands while
|
|
402
|
-
// there is already a command
|
|
403
|
-
//
|
|
400
|
+
// there is already a command executing.
|
|
401
|
+
// The alternative would be to queue commands
|
|
404
402
|
// but there is no guarantee that the state after one command executes will still be compatible
|
|
405
403
|
// with the next one. In fact, it is likely not to be.
|
|
406
404
|
return [2 /*return*/];
|
|
@@ -411,7 +409,7 @@ var CommandController = /** @class */ (function () {
|
|
|
411
409
|
}
|
|
412
410
|
executeOptions = {
|
|
413
411
|
initialState: this.textController.getState(),
|
|
414
|
-
textApi: this.textController
|
|
412
|
+
textApi: this.textController,
|
|
415
413
|
};
|
|
416
414
|
if (!(((_a = command.shouldUndo) === null || _a === void 0 ? void 0 : _a.call(command, executeOptions)) && (command === null || command === void 0 ? void 0 : command.undo))) return [3 /*break*/, 1];
|
|
417
415
|
command.undo(executeOptions);
|
|
@@ -435,7 +433,7 @@ var TextAreaTextController = /** @class */ (function () {
|
|
|
435
433
|
TextAreaTextController.prototype.replaceSelection = function (text) {
|
|
436
434
|
var textArea = this.textAreaRef.current;
|
|
437
435
|
if (!textArea) {
|
|
438
|
-
throw new Error(
|
|
436
|
+
throw new Error('TextAreaRef is not set');
|
|
439
437
|
}
|
|
440
438
|
insertText(textArea, text);
|
|
441
439
|
return getStateFromTextArea(textArea);
|
|
@@ -443,7 +441,7 @@ var TextAreaTextController = /** @class */ (function () {
|
|
|
443
441
|
TextAreaTextController.prototype.setSelectionRange = function (selection) {
|
|
444
442
|
var textArea = this.textAreaRef.current;
|
|
445
443
|
if (!textArea) {
|
|
446
|
-
throw new Error(
|
|
444
|
+
throw new Error('TextAreaRef is not set');
|
|
447
445
|
}
|
|
448
446
|
textArea.focus();
|
|
449
447
|
textArea.selectionStart = selection.start;
|
|
@@ -453,7 +451,7 @@ var TextAreaTextController = /** @class */ (function () {
|
|
|
453
451
|
TextAreaTextController.prototype.getState = function () {
|
|
454
452
|
var textArea = this.textAreaRef.current;
|
|
455
453
|
if (!textArea) {
|
|
456
|
-
throw new Error(
|
|
454
|
+
throw new Error('TextAreaRef is not set');
|
|
457
455
|
}
|
|
458
456
|
return getStateFromTextArea(textArea);
|
|
459
457
|
};
|
|
@@ -463,9 +461,9 @@ function getStateFromTextArea(textArea) {
|
|
|
463
461
|
return {
|
|
464
462
|
selection: {
|
|
465
463
|
start: textArea.selectionStart,
|
|
466
|
-
end: textArea.selectionEnd
|
|
464
|
+
end: textArea.selectionEnd,
|
|
467
465
|
},
|
|
468
|
-
text: textArea.value
|
|
466
|
+
text: textArea.value,
|
|
469
467
|
};
|
|
470
468
|
}
|
|
471
469
|
/**
|
|
@@ -489,12 +487,12 @@ function insertText(input, text) {
|
|
|
489
487
|
return;
|
|
490
488
|
}
|
|
491
489
|
// Webkit + Edge
|
|
492
|
-
var isSuccess = document.execCommand(
|
|
490
|
+
var isSuccess = document.execCommand('insertText', false, text);
|
|
493
491
|
if (!isSuccess) {
|
|
494
492
|
var start = input.selectionStart || 0;
|
|
495
493
|
var end = input.selectionEnd || 0;
|
|
496
494
|
// Firefox (non-standard method)
|
|
497
|
-
if (typeof input.setRangeText ===
|
|
495
|
+
if (typeof input.setRangeText === 'function') {
|
|
498
496
|
input.setRangeText(text);
|
|
499
497
|
}
|
|
500
498
|
else {
|
|
@@ -543,8 +541,8 @@ function insertText(input, text) {
|
|
|
543
541
|
// Correct the cursor position to be at the end of the insertion
|
|
544
542
|
input.setSelectionRange(start + text.length, start + text.length);
|
|
545
543
|
// Notify any possible listeners of the change
|
|
546
|
-
var e = document.createEvent(
|
|
547
|
-
e.initEvent(
|
|
544
|
+
var e = document.createEvent('UIEvent');
|
|
545
|
+
e.initEvent('input', true, false);
|
|
548
546
|
input.dispatchEvent(e);
|
|
549
547
|
}
|
|
550
548
|
}
|
|
@@ -554,13 +552,13 @@ function insertText(input, text) {
|
|
|
554
552
|
* Copied from https://github.com/grassator/insert-text-at-cursor
|
|
555
553
|
*/
|
|
556
554
|
function canManipulateViaTextNodes(input) {
|
|
557
|
-
if (input.nodeName !==
|
|
555
|
+
if (input.nodeName !== 'TEXTAREA') {
|
|
558
556
|
return false;
|
|
559
557
|
}
|
|
560
558
|
var browserSupportsTextareaTextNodes;
|
|
561
|
-
if (typeof browserSupportsTextareaTextNodes ===
|
|
562
|
-
var textarea = document.createElement(
|
|
563
|
-
textarea.value =
|
|
559
|
+
if (typeof browserSupportsTextareaTextNodes === 'undefined') {
|
|
560
|
+
var textarea = document.createElement('textarea');
|
|
561
|
+
textarea.value = '1';
|
|
564
562
|
browserSupportsTextareaTextNodes = !!textarea.firstChild;
|
|
565
563
|
}
|
|
566
564
|
return browserSupportsTextareaTextNodes;
|
|
@@ -570,25 +568,23 @@ function makeList(state0, textController, insertBefore) {
|
|
|
570
568
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
571
569
|
var newSelectionRange = selectWord({
|
|
572
570
|
text: state0.text,
|
|
573
|
-
selection: state0.selection
|
|
571
|
+
selection: state0.selection,
|
|
574
572
|
});
|
|
575
573
|
var state1 = textController.setSelectionRange(newSelectionRange);
|
|
576
574
|
var breaksBeforeCount = getBreaksNeededForEmptyLineBefore(state1.text, state1.selection.start);
|
|
577
|
-
var breaksBefore = Array(breaksBeforeCount + 1).join(
|
|
575
|
+
var breaksBefore = Array(breaksBeforeCount + 1).join('\n');
|
|
578
576
|
var breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);
|
|
579
|
-
var breaksAfter = Array(breaksAfterCount + 1).join(
|
|
577
|
+
var breaksAfter = Array(breaksAfterCount + 1).join('\n');
|
|
580
578
|
var modifiedText = insertBeforeEachLine(getSelectedText(state1), insertBefore);
|
|
581
579
|
textController.replaceSelection("".concat(breaksBefore).concat(modifiedText.modifiedText).concat(breaksAfter));
|
|
582
580
|
// Specifically when the text has only one line, we can exclude the "- ", for example, from the selection
|
|
583
|
-
var oneLinerOffset = getSelectedText(state1).indexOf(
|
|
584
|
-
? modifiedText.insertionLength
|
|
585
|
-
: 0;
|
|
581
|
+
var oneLinerOffset = getSelectedText(state1).indexOf('\n') === -1 ? modifiedText.insertionLength : 0;
|
|
586
582
|
var selectionStart = state1.selection.start + breaksBeforeCount + oneLinerOffset;
|
|
587
583
|
var selectionEnd = selectionStart + modifiedText.modifiedText.length - oneLinerOffset;
|
|
588
584
|
// Adjust the selection to not contain the **
|
|
589
585
|
textController.setSelectionRange({
|
|
590
586
|
start: selectionStart,
|
|
591
|
-
end: selectionEnd
|
|
587
|
+
end: selectionEnd,
|
|
592
588
|
});
|
|
593
589
|
}
|
|
594
590
|
|
|
@@ -599,15 +595,15 @@ var listHelpers = /*#__PURE__*/Object.freeze({
|
|
|
599
595
|
|
|
600
596
|
var codeCommand = {
|
|
601
597
|
shouldUndo: function (options) {
|
|
602
|
-
return (getCharactersBeforeSelection(options.initialState, 1) ===
|
|
603
|
-
getCharactersAfterSelection(options.initialState, 1) ===
|
|
598
|
+
return (getCharactersBeforeSelection(options.initialState, 1) === '`' &&
|
|
599
|
+
getCharactersAfterSelection(options.initialState, 1) === '`');
|
|
604
600
|
},
|
|
605
601
|
execute: function (_a) {
|
|
606
602
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
607
603
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
608
604
|
var newSelectionRange = selectWord({
|
|
609
605
|
text: initialState.text,
|
|
610
|
-
selection: initialState.selection
|
|
606
|
+
selection: initialState.selection,
|
|
611
607
|
});
|
|
612
608
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
613
609
|
// Replaces the current selection with the italic mark up
|
|
@@ -615,7 +611,7 @@ var codeCommand = {
|
|
|
615
611
|
// Adjust the selection to not contain the *
|
|
616
612
|
textApi.setSelectionRange({
|
|
617
613
|
start: state2.selection.end - 1 - getSelectedText(state1).length,
|
|
618
|
-
end: state2.selection.end - 1
|
|
614
|
+
end: state2.selection.end - 1,
|
|
619
615
|
});
|
|
620
616
|
},
|
|
621
617
|
undo: function (_a) {
|
|
@@ -623,14 +619,14 @@ var codeCommand = {
|
|
|
623
619
|
var text = getSelectedText(initialState);
|
|
624
620
|
textApi.setSelectionRange({
|
|
625
621
|
start: initialState.selection.start - 1,
|
|
626
|
-
end: initialState.selection.end + 1
|
|
622
|
+
end: initialState.selection.end + 1,
|
|
627
623
|
});
|
|
628
624
|
textApi.replaceSelection(text);
|
|
629
625
|
textApi.setSelectionRange({
|
|
630
626
|
start: initialState.selection.start - 1,
|
|
631
|
-
end: initialState.selection.end - 1
|
|
627
|
+
end: initialState.selection.end - 1,
|
|
632
628
|
});
|
|
633
|
-
}
|
|
629
|
+
},
|
|
634
630
|
};
|
|
635
631
|
|
|
636
632
|
function useTextAreaMarkdownEditor(options) {
|
|
@@ -642,7 +638,7 @@ function useTextAreaMarkdownEditor(options) {
|
|
|
642
638
|
return {
|
|
643
639
|
textController: textController,
|
|
644
640
|
commandController: commandController,
|
|
645
|
-
ref: textAreaRef
|
|
641
|
+
ref: textAreaRef,
|
|
646
642
|
};
|
|
647
643
|
}
|
|
648
644
|
|
|
@@ -654,91 +650,91 @@ var codeBlockCommand = {
|
|
|
654
650
|
return __generator(this, function (_b) {
|
|
655
651
|
newSelectionRange = selectWord({
|
|
656
652
|
text: initialState.text,
|
|
657
|
-
selection: initialState.selection
|
|
653
|
+
selection: initialState.selection,
|
|
658
654
|
});
|
|
659
655
|
state1 = textApi.setSelectionRange(newSelectionRange);
|
|
660
656
|
// when there's no breaking line
|
|
661
|
-
if (getSelectedText(state1).indexOf(
|
|
657
|
+
if (getSelectedText(state1).indexOf('\n') === -1) {
|
|
662
658
|
textApi.replaceSelection("`".concat(getSelectedText(state1), "`"));
|
|
663
659
|
selectionStart_1 = state1.selection.start + 1;
|
|
664
660
|
selectionEnd_1 = selectionStart_1 + getSelectedText(state1).length;
|
|
665
661
|
textApi.setSelectionRange({
|
|
666
662
|
start: selectionStart_1,
|
|
667
|
-
end: selectionEnd_1
|
|
663
|
+
end: selectionEnd_1,
|
|
668
664
|
});
|
|
669
665
|
return [2 /*return*/];
|
|
670
666
|
}
|
|
671
667
|
breaksBeforeCount = getBreaksNeededForEmptyLineBefore(state1.text, state1.selection.start);
|
|
672
|
-
breaksBefore = Array(breaksBeforeCount + 1).join(
|
|
668
|
+
breaksBefore = Array(breaksBeforeCount + 1).join('\n');
|
|
673
669
|
breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);
|
|
674
|
-
breaksAfter = Array(breaksAfterCount + 1).join(
|
|
670
|
+
breaksAfter = Array(breaksAfterCount + 1).join('\n');
|
|
675
671
|
textApi.replaceSelection("".concat(breaksBefore, "```\n").concat(getSelectedText(state1), "\n```").concat(breaksAfter));
|
|
676
672
|
selectionStart = state1.selection.start + breaksBeforeCount + 4;
|
|
677
673
|
selectionEnd = selectionStart + getSelectedText(state1).length;
|
|
678
674
|
textApi.setSelectionRange({
|
|
679
675
|
start: selectionStart,
|
|
680
|
-
end: selectionEnd
|
|
676
|
+
end: selectionEnd,
|
|
681
677
|
});
|
|
682
678
|
return [2 /*return*/];
|
|
683
679
|
});
|
|
684
680
|
});
|
|
685
|
-
}
|
|
681
|
+
},
|
|
686
682
|
};
|
|
687
683
|
|
|
688
684
|
var checkedListCommand = {
|
|
689
685
|
execute: function (_a) {
|
|
690
686
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
691
687
|
makeList(initialState, textApi, function () { return "- [ ] "; });
|
|
692
|
-
}
|
|
688
|
+
},
|
|
693
689
|
};
|
|
694
690
|
|
|
695
691
|
var orderedListCommand = {
|
|
696
692
|
execute: function (_a) {
|
|
697
693
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
698
694
|
makeList(initialState, textApi, function (item, index) { return "".concat(index + 1, ". "); });
|
|
699
|
-
}
|
|
695
|
+
},
|
|
700
696
|
};
|
|
701
697
|
|
|
702
698
|
var unorderedListCommand = {
|
|
703
699
|
execute: function (_a) {
|
|
704
700
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
705
|
-
makeList(initialState, textApi,
|
|
706
|
-
}
|
|
701
|
+
makeList(initialState, textApi, '- ');
|
|
702
|
+
},
|
|
707
703
|
};
|
|
708
704
|
|
|
709
705
|
var headingLevel2Command = {
|
|
710
706
|
execute: function (_a) {
|
|
711
707
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
712
|
-
setHeader(initialState, textApi,
|
|
713
|
-
}
|
|
708
|
+
setHeader(initialState, textApi, '## ');
|
|
709
|
+
},
|
|
714
710
|
};
|
|
715
711
|
|
|
716
712
|
var headingLevel3Command = {
|
|
717
713
|
execute: function (_a) {
|
|
718
714
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
719
|
-
setHeader(initialState, textApi,
|
|
720
|
-
}
|
|
715
|
+
setHeader(initialState, textApi, '### ');
|
|
716
|
+
},
|
|
721
717
|
};
|
|
722
718
|
|
|
723
719
|
var headingLevel4Command = {
|
|
724
720
|
execute: function (_a) {
|
|
725
721
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
726
|
-
setHeader(initialState, textApi,
|
|
727
|
-
}
|
|
722
|
+
setHeader(initialState, textApi, '#### ');
|
|
723
|
+
},
|
|
728
724
|
};
|
|
729
725
|
|
|
730
726
|
var headingLevel5Command = {
|
|
731
727
|
execute: function (_a) {
|
|
732
728
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
733
|
-
setHeader(initialState, textApi,
|
|
734
|
-
}
|
|
729
|
+
setHeader(initialState, textApi, '##### ');
|
|
730
|
+
},
|
|
735
731
|
};
|
|
736
732
|
|
|
737
733
|
var headingLevel6Command = {
|
|
738
734
|
execute: function (_a) {
|
|
739
735
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
740
|
-
setHeader(initialState, textApi,
|
|
741
|
-
}
|
|
736
|
+
setHeader(initialState, textApi, '###### ');
|
|
737
|
+
},
|
|
742
738
|
};
|
|
743
739
|
|
|
744
740
|
exports.CommandController = CommandController;
|