tiny-markdown-editor 0.1.4 → 0.1.6

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.
@@ -1,70 +1,13 @@
1
1
  "use strict";
2
2
 
3
- require("core-js/modules/es.symbol");
4
-
5
- require("core-js/modules/es.symbol.description");
6
-
7
- require("core-js/modules/es.symbol.iterator");
8
-
9
- require("core-js/modules/es.array.concat");
10
-
11
- require("core-js/modules/es.array.from");
12
-
13
- require("core-js/modules/es.array.iterator");
14
-
15
- require("core-js/modules/es.array.join");
16
-
17
- require("core-js/modules/es.array.slice");
18
-
19
- require("core-js/modules/es.function.name");
20
-
21
- require("core-js/modules/es.object.assign");
22
-
23
- require("core-js/modules/es.object.to-string");
24
-
25
- require("core-js/modules/es.regexp.exec");
26
-
27
- require("core-js/modules/es.regexp.to-string");
28
-
29
- require("core-js/modules/es.string.iterator");
30
-
31
- require("core-js/modules/es.string.match");
32
-
33
- require("core-js/modules/es.string.split");
34
-
35
- require("core-js/modules/es.string.anchor");
36
-
37
- require("core-js/modules/es.string.bold");
38
-
39
- require("core-js/modules/es.string.link");
40
-
41
- require("core-js/modules/web.dom-collections.iterator");
42
-
43
3
  Object.defineProperty(exports, "__esModule", {
44
4
  value: true
45
5
  });
46
6
  exports.default = void 0;
47
-
48
7
  var _svg = _interopRequireDefault(require("./svg/svg"));
49
-
50
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
51
-
52
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
53
-
54
- function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
55
-
56
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
57
-
58
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
59
-
60
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
61
-
62
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
63
-
64
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
65
-
66
- var isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
67
- var DefaultCommands = {
9
+ const isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
10
+ const DefaultCommands = {
68
11
  'bold': {
69
12
  name: 'bold',
70
13
  action: 'bold',
@@ -127,296 +70,196 @@ var DefaultCommands = {
127
70
  },
128
71
  'insertLink': {
129
72
  name: 'insertLink',
130
- action: function action(editor) {
73
+ action: editor => {
131
74
  if (editor.isInlineFormattingAllowed()) editor.wrapSelection('[', ']()');
132
75
  },
133
- enabled: function enabled(editor, focus, anchor) {
134
- return editor.isInlineFormattingAllowed(focus, anchor) ? false : null;
135
- },
76
+ enabled: (editor, focus, anchor) => editor.isInlineFormattingAllowed(focus, anchor) ? false : null,
136
77
  innerHTML: _svg.default.link,
137
78
  title: 'Insert link',
138
79
  hotkey: 'Mod-K'
139
80
  },
140
81
  'insertImage': {
141
82
  name: 'insertImage',
142
- action: function action(editor) {
83
+ action: editor => {
143
84
  if (editor.isInlineFormattingAllowed()) editor.wrapSelection('![', ']()');
144
85
  },
145
- enabled: function enabled(editor, focus, anchor) {
146
- return editor.isInlineFormattingAllowed(focus, anchor) ? false : null;
147
- },
86
+ enabled: (editor, focus, anchor) => editor.isInlineFormattingAllowed(focus, anchor) ? false : null,
148
87
  innerHTML: _svg.default.image,
149
88
  title: 'Insert image',
150
89
  hotkey: 'Mod2-Shift-I'
151
90
  },
152
91
  'hr': {
153
92
  name: 'hr',
154
- action: function action(editor) {
155
- return editor.paste('\n***\n');
156
- },
157
- enabled: function enabled() {
158
- return false;
159
- },
93
+ action: editor => editor.paste('\n***\n'),
94
+ enabled: () => false,
160
95
  innerHTML: _svg.default.hr,
161
96
  title: 'Insert horizontal line',
162
97
  hotkey: 'Mod2-Shift-L'
163
98
  }
164
99
  };
165
-
166
- var CommandBar = /*#__PURE__*/function () {
167
- function CommandBar(props) {
168
- var _this = this;
169
-
170
- _classCallCheck(this, CommandBar);
171
-
100
+ class CommandBar {
101
+ constructor(props) {
172
102
  this.e = null;
173
103
  this.editor = null;
174
104
  this.commands = [];
175
105
  this.buttons = {};
176
106
  this.state = {};
177
107
  this.hotkeys = [];
178
- var element = props.element;
179
-
108
+ let element = props.element;
180
109
  if (element && !element.tagName) {
181
110
  element = document.getElementById(props.element);
182
111
  }
183
-
184
112
  if (!element) {
185
113
  element = document.body;
186
114
  }
187
-
188
115
  this.createCommandBarElement(element, props.commands || ['bold', 'italic', 'strikethrough', '|', 'code', '|', 'h1', 'h2', '|', 'ul', 'ol', '|', 'blockquote', 'hr', '|', 'insertLink', 'insertImage']);
189
- document.addEventListener('keydown', function (e) {
190
- return _this.handleKeydown(e);
191
- });
116
+ document.addEventListener('keydown', e => this.handleKeydown(e));
192
117
  if (props.editor) this.setEditor(props.editor);
193
118
  }
194
-
195
- _createClass(CommandBar, [{
196
- key: "createCommandBarElement",
197
- value: function createCommandBarElement(parentElement, commands) {
198
- var _this2 = this;
199
-
200
- this.e = document.createElement('div');
201
- this.e.className = 'TMCommandBar';
202
-
203
- var _iterator = _createForOfIteratorHelper(commands),
204
- _step;
205
-
206
- try {
207
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
208
- var command = _step.value;
209
-
210
- if (command == '|') {
211
- var el = document.createElement('div');
212
- el.className = 'TMCommandDivider';
213
- this.e.appendChild(el);
119
+ createCommandBarElement(parentElement, commands) {
120
+ this.e = document.createElement('div');
121
+ this.e.className = 'TMCommandBar';
122
+ for (let command of commands) {
123
+ if (command == '|') {
124
+ let el = document.createElement('div');
125
+ el.className = 'TMCommandDivider';
126
+ this.e.appendChild(el);
127
+ } else {
128
+ let commandName;
129
+ if (typeof command == "string") {
130
+ // Reference to default command
131
+
132
+ if (DefaultCommands[command]) {
133
+ commandName = command;
134
+ this.commands[commandName] = DefaultCommands[commandName];
214
135
  } else {
215
- var _ret = function () {
216
- var commandName = void 0;
217
-
218
- if (typeof command == "string") {
219
- // Reference to default command
220
- if (DefaultCommands[command]) {
221
- commandName = command;
222
- _this2.commands[commandName] = DefaultCommands[commandName];
223
- } else {
224
- return "continue";
225
- }
226
- } else if (_typeof(command) == "object" && command.name) {
227
- commandName = command.name;
228
- _this2.commands[commandName] = {};
229
- if (DefaultCommands[commandName]) Object.assign(_this2.commands[commandName], DefaultCommands[commandName]);
230
- Object.assign(_this2.commands[commandName], command);
231
- } else {
232
- return "continue";
233
- }
234
-
235
- var title = _this2.commands[commandName].title || commandName;
236
-
237
- if (_this2.commands[commandName].hotkey) {
238
- var keys = _this2.commands[commandName].hotkey.split('-'); // construct modifiers
239
-
240
-
241
- var modifiers = [];
242
- var modifierexplanation = [];
243
-
244
- for (var i = 0; i < keys.length - 1; i++) {
245
- switch (keys[i]) {
246
- case 'Ctrl':
247
- modifiers.push('ctrlKey');
248
- modifierexplanation.push('Ctrl');
249
- break;
250
-
251
- case 'Cmd':
252
- modifiers.push('metaKey');
253
- modifierexplanation.push('');
254
- break;
255
-
256
- case 'Alt':
257
- modifiers.push('altKey');
258
- modifierexplanation.push('Alt');
259
- break;
260
-
261
- case 'Option':
262
- modifiers.push('altKey');
263
- modifierexplanation.push('⌥');
264
- break;
265
-
266
- case 'Win':
267
- modifiers.push('metaKey');
268
- modifierexplanation.push('⊞ Win');
269
- break;
270
-
271
- case 'Shift':
272
- modifiers.push('shiftKey');
273
- modifierexplanation.push('⇧');
274
- break;
275
-
276
- case 'Mod':
277
- // Mod is a convenience mechanism: Ctrl on Windows, Cmd on Mac
278
- if (isMacLike) {
279
- modifiers.push('metaKey');
280
- modifierexplanation.push('⌘');
281
- } else {
282
- modifiers.push('ctrlKey');
283
- modifierexplanation.push('Ctrl');
284
- }
285
-
286
- break;
287
-
288
- case 'Mod2':
289
- modifiers.push('altKey');
290
- if (isMacLike) modifierexplanation.push('⌥');else modifierexplanation.push('Alt');
291
- break;
292
- // Mod2 is a convenience mechanism: Alt on Windows, Option on Mac
293
- }
294
- }
295
-
296
- modifierexplanation.push(keys[keys.length - 1]);
297
- var hotkey = {
298
- modifiers: modifiers,
299
- command: commandName
300
- }; // TODO Right now this is working only for letters and numbers
301
-
302
- if (keys[keys.length - 1].match(/^[0-9]$/)) {
303
- hotkey.code = "Digit".concat(keys[keys.length - 1]);
136
+ continue;
137
+ }
138
+ } else if (typeof command == "object" && command.name) {
139
+ commandName = command.name;
140
+ this.commands[commandName] = {};
141
+ if (DefaultCommands[commandName]) Object.assign(this.commands[commandName], DefaultCommands[commandName]);
142
+ Object.assign(this.commands[commandName], command);
143
+ } else {
144
+ continue;
145
+ }
146
+ let title = this.commands[commandName].title || commandName;
147
+ if (this.commands[commandName].hotkey) {
148
+ const keys = this.commands[commandName].hotkey.split('-');
149
+ // construct modifiers
150
+ let modifiers = [];
151
+ let modifierexplanation = [];
152
+ for (let i = 0; i < keys.length - 1; i++) {
153
+ switch (keys[i]) {
154
+ case 'Ctrl':
155
+ modifiers.push('ctrlKey');
156
+ modifierexplanation.push('Ctrl');
157
+ break;
158
+ case 'Cmd':
159
+ modifiers.push('metaKey');
160
+ modifierexplanation.push('⌘');
161
+ break;
162
+ case 'Alt':
163
+ modifiers.push('altKey');
164
+ modifierexplanation.push('Alt');
165
+ break;
166
+ case 'Option':
167
+ modifiers.push('altKey');
168
+ modifierexplanation.push('');
169
+ break;
170
+ case 'Win':
171
+ modifiers.push('metaKey');
172
+ modifierexplanation.push('⊞ Win');
173
+ break;
174
+ case 'Shift':
175
+ modifiers.push('shiftKey');
176
+ modifierexplanation.push('⇧');
177
+ break;
178
+ case 'Mod':
179
+ // Mod is a convenience mechanism: Ctrl on Windows, Cmd on Mac
180
+ if (isMacLike) {
181
+ modifiers.push('metaKey');
182
+ modifierexplanation.push('');
304
183
  } else {
305
- hotkey.key = keys[keys.length - 1].toLowerCase();
184
+ modifiers.push('ctrlKey');
185
+ modifierexplanation.push('Ctrl');
306
186
  }
187
+ break;
188
+ case 'Mod2':
189
+ modifiers.push('altKey');
190
+ if (isMacLike) modifierexplanation.push('⌥');else modifierexplanation.push('Alt');
191
+ break;
192
+ // Mod2 is a convenience mechanism: Alt on Windows, Option on Mac
193
+ }
194
+ }
307
195
 
308
- _this2.hotkeys.push(hotkey);
309
-
310
- title = title.concat(" (".concat(modifierexplanation.join('+'), ")"));
311
- }
312
-
313
- _this2.buttons[commandName] = document.createElement('div');
314
- _this2.buttons[commandName].className = 'TMCommandButton TMCommandButton_Disabled';
315
- _this2.buttons[commandName].title = title;
316
- _this2.buttons[commandName].innerHTML = _this2.commands[commandName].innerHTML;
317
-
318
- _this2.buttons[commandName].addEventListener('mousedown', function (e) {
319
- return _this2.handleClick(commandName, e);
320
- });
321
-
322
- _this2.e.appendChild(_this2.buttons[commandName]);
323
- }();
324
-
325
- if (_ret === "continue") continue;
196
+ modifierexplanation.push(keys[keys.length - 1]);
197
+ let hotkey = {
198
+ modifiers: modifiers,
199
+ command: commandName
200
+ };
201
+ // TODO Right now this is working only for letters and numbers
202
+ if (keys[keys.length - 1].match(/^[0-9]$/)) {
203
+ hotkey.code = `Digit${keys[keys.length - 1]}`;
204
+ } else {
205
+ hotkey.key = keys[keys.length - 1].toLowerCase();
326
206
  }
207
+ this.hotkeys.push(hotkey);
208
+ title = title.concat(` (${modifierexplanation.join('+')})`);
327
209
  }
328
- } catch (err) {
329
- _iterator.e(err);
330
- } finally {
331
- _iterator.f();
210
+ this.buttons[commandName] = document.createElement('div');
211
+ this.buttons[commandName].className = 'TMCommandButton TMCommandButton_Disabled';
212
+ this.buttons[commandName].title = title;
213
+ this.buttons[commandName].innerHTML = this.commands[commandName].innerHTML;
214
+ this.buttons[commandName].addEventListener('mousedown', e => this.handleClick(commandName, e));
215
+ this.e.appendChild(this.buttons[commandName]);
332
216
  }
333
-
334
- parentElement.appendChild(this.e);
335
217
  }
336
- }, {
337
- key: "handleClick",
338
- value: function handleClick(commandName, event) {
339
- if (!this.editor) return;
340
- event.preventDefault();
341
-
342
- if (typeof this.commands[commandName].action == "string") {
343
- if (this.state[commandName] === false) this.editor.setCommandState(commandName, true);else this.editor.setCommandState(commandName, false);
344
- } else if (typeof this.commands[commandName].action == "function") {
345
- this.commands[commandName].action(this.editor);
346
- }
347
- }
348
- }, {
349
- key: "setEditor",
350
- value: function setEditor(editor) {
351
- var _this3 = this;
352
-
353
- this.editor = editor;
354
- editor.addEventListener('selection', function (e) {
355
- return _this3.handleSelection(e);
356
- });
218
+ parentElement.appendChild(this.e);
219
+ }
220
+ handleClick(commandName, event) {
221
+ if (!this.editor) return;
222
+ event.preventDefault();
223
+ if (typeof this.commands[commandName].action == "string") {
224
+ if (this.state[commandName] === false) this.editor.setCommandState(commandName, true);else this.editor.setCommandState(commandName, false);
225
+ } else if (typeof this.commands[commandName].action == "function") {
226
+ this.commands[commandName].action(this.editor);
357
227
  }
358
- }, {
359
- key: "handleSelection",
360
- value: function handleSelection(event) {
361
- if (event.commandState) {
362
- for (var command in this.commands) {
363
- if (event.commandState[command] === undefined) {
364
- if (this.commands[command].enabled) this.state[command] = this.commands[command].enabled(this.editor, event.focus, event.anchor);else this.state[command] = event.focus ? false : null;
365
- } else {
366
- this.state[command] = event.commandState[command];
367
- }
368
-
369
- if (this.state[command] === true) {
370
- this.buttons[command].className = 'TMCommandButton TMCommandButton_Active';
371
- } else if (this.state[command] === false) {
372
- this.buttons[command].className = 'TMCommandButton TMCommandButton_Inactive';
373
- } else {
374
- this.buttons[command].className = 'TMCommandButton TMCommandButton_Disabled';
375
- }
228
+ }
229
+ setEditor(editor) {
230
+ this.editor = editor;
231
+ editor.addEventListener('selection', e => this.handleSelection(e));
232
+ }
233
+ handleSelection(event) {
234
+ if (event.commandState) {
235
+ for (let command in this.commands) {
236
+ if (event.commandState[command] === undefined) {
237
+ if (this.commands[command].enabled) this.state[command] = this.commands[command].enabled(this.editor, event.focus, event.anchor);else this.state[command] = event.focus ? false : null;
238
+ } else {
239
+ this.state[command] = event.commandState[command];
240
+ }
241
+ if (this.state[command] === true) {
242
+ this.buttons[command].className = 'TMCommandButton TMCommandButton_Active';
243
+ } else if (this.state[command] === false) {
244
+ this.buttons[command].className = 'TMCommandButton TMCommandButton_Inactive';
245
+ } else {
246
+ this.buttons[command].className = 'TMCommandButton TMCommandButton_Disabled';
376
247
  }
377
248
  }
378
249
  }
379
- }, {
380
- key: "handleKeydown",
381
- value: function handleKeydown(event) {
382
- var _iterator2 = _createForOfIteratorHelper(this.hotkeys),
383
- _step2;
384
-
385
- try {
386
- outer: for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
387
- var hotkey = _step2.value;
388
-
389
- if (hotkey.key && event.key.toLowerCase() == hotkey.key || hotkey.code && event.code == hotkey.code) {
390
- // Key matches hotkey. Look for any required modifier that wasn't pressed
391
- var _iterator3 = _createForOfIteratorHelper(hotkey.modifiers),
392
- _step3;
393
-
394
- try {
395
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
396
- var modifier = _step3.value;
397
- if (!event[modifier]) continue outer;
398
- } // Everything matches.
399
-
400
- } catch (err) {
401
- _iterator3.e(err);
402
- } finally {
403
- _iterator3.f();
404
- }
405
-
406
- this.handleClick(hotkey.command, event);
407
- return;
408
- }
250
+ }
251
+ handleKeydown(event) {
252
+ outer: for (let hotkey of this.hotkeys) {
253
+ if (hotkey.key && event.key.toLowerCase() == hotkey.key || hotkey.code && event.code == hotkey.code) {
254
+ // Key matches hotkey. Look for any required modifier that wasn't pressed
255
+ for (let modifier of hotkey.modifiers) {
256
+ if (!event[modifier]) continue outer;
409
257
  }
410
- } catch (err) {
411
- _iterator2.e(err);
412
- } finally {
413
- _iterator2.f();
258
+ // Everything matches.
259
+ this.handleClick(hotkey.command, event);
260
+ return;
414
261
  }
415
262
  }
416
- }]);
417
-
418
- return CommandBar;
419
- }();
420
-
421
- var _default = CommandBar;
422
- exports.default = _default;
263
+ }
264
+ }
265
+ var _default = exports.default = CommandBar;