This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
/
gh-markdown-editor.js
604 lines (505 loc) · 20.4 KB
/
gh-markdown-editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import Component from '@ember/component';
import ShortcutsMixin from 'ghost-admin/mixins/shortcuts';
import ctrlOrCmd from 'ghost-admin/utils/ctrl-or-cmd';
import formatMarkdown from 'ghost-admin/utils/format-markdown';
import {assign} from '@ember/polyfills';
import {computed} from '@ember/object';
import {copy} from '@ember/object/internals';
import {htmlSafe} from '@ember/string';
import {inject as injectService} from '@ember/service';
import {isEmpty, typeOf} from '@ember/utils';
import {run} from '@ember/runloop';
const MOBILEDOC_VERSION = '0.3.1';
export const BLANK_DOC = {
version: MOBILEDOC_VERSION,
markups: [],
atoms: [],
cards: [
['card-markdown', {
cardName: 'card-markdown',
markdown: ''
}]
],
sections: [[10, 0]]
};
export default Component.extend(ShortcutsMixin, {
config: injectService(),
notifications: injectService(),
settings: injectService(),
classNames: ['gh-markdown-editor'],
classNameBindings: [
'_isFullScreen:gh-markdown-editor-full-screen',
'_isSplitScreen:gh-markdown-editor-side-by-side'
],
// Public attributes
autofocus: false,
imageMimeTypes: null,
isFullScreen: false,
mobiledoc: null,
options: null,
placeholder: '',
uploadedImageUrls: null,
// Closure actions
onChange() {},
onFullScreenToggle() {},
onImageFilesSelected() {},
onPreviewToggle() {},
onSplitScreenToggle() {},
showMarkdownHelp() {},
// Internal attributes
markdown: null,
// Private
_editor: null,
_editorFocused: false,
_isFullScreen: false,
_isSplitScreen: false,
_isHemmingwayMode: false,
_isUploading: false,
_showUnsplash: false,
_statusbar: null,
_toolbar: null,
_uploadedImageUrls: null,
simpleMDEOptions: computed('options', function () {
let options = this.get('options') || {};
let defaultOptions = {
// use our Showdown config with sanitization for previews
previewRender(markdown) {
return formatMarkdown(markdown);
},
// Ghost-specific SimpleMDE toolbar config - allows us to create a
// bridge between SimpleMDE buttons and Ember actions
toolbar: [
'bold', 'italic', 'heading', '|',
'quote', 'unordered-list', 'ordered-list', '|',
'link',
{
name: 'image',
action: () => {
this._openImageFileDialog();
},
className: 'fa fa-picture-o',
title: 'Upload Image(s)'
},
'|',
{
name: 'preview',
action: () => {
this._togglePreview();
},
className: 'fa fa-eye no-disable',
title: 'Toggle Preview'
},
{
name: 'side-by-side',
action: () => {
this.send('toggleSplitScreen');
},
className: 'fa fa-columns no-disable no-mobile',
title: 'Toggle Side-by-side Preview'
},
'|',
{
name: 'spellcheck',
action: () => {
this._toggleSpellcheck();
},
className: 'fa fa-check',
title: 'Toggle Spellcheck'
},
{
name: 'hemmingway',
action: () => {
this._toggleHemmingway();
},
className: 'fa fa-h-square',
title: 'Toggle Hemmingway Mode'
},
{
name: 'guide',
action: () => {
this.showMarkdownHelp();
},
className: 'fa fa-question-circle',
title: 'Markdown Guide'
}
],
// disable shortcuts for side-by-side and fullscreen because they
// trigger interal SimpleMDE methods that will result in broken
// layouts
shortcuts: {
toggleFullScreen: null,
togglePreview: null,
toggleSideBySide: null,
drawImage: null
},
// only include the number of words in the status bar
status: ['words']
};
if (this.get('settings.unsplash.isActive')) {
let image = defaultOptions.toolbar.findBy('name', 'image');
let index = defaultOptions.toolbar.indexOf(image) + 1;
defaultOptions.toolbar.splice(index, 0, {
name: 'unsplash',
action: () => {
this.send('toggleUnsplash');
},
className: 'fa fa-camera',
title: 'Add Image from Unsplash'
});
}
return assign(defaultOptions, options);
}),
shortcuts: {},
init() {
this._super(...arguments);
let shortcuts = this.get('shortcuts');
shortcuts[`${ctrlOrCmd}+shift+i`] = {action: 'openImageFileDialog'};
shortcuts['ctrl+alt+h'] = {action: 'toggleHemmingway'};
},
// extract markdown content from single markdown card
didReceiveAttrs() {
this._super(...arguments);
let mobiledoc = this.get('mobiledoc') || copy(BLANK_DOC, true);
let uploadedImageUrls = this.get('uploadedImageUrls');
if (!isEmpty(uploadedImageUrls) && uploadedImageUrls !== this._uploadedImageUrls) {
this._uploadedImageUrls = uploadedImageUrls;
// must be done afterRender to avoid double modify of mobiledoc in
// a single render
run.scheduleOnce('afterRender', this, () => {
this._insertImages(uploadedImageUrls);
// reset the file input so the same file can be selected again
this.$('input[type=file]').val('');
});
}
// eslint-disable-next-line ember-suave/prefer-destructuring
let markdown = mobiledoc.cards[0][1].markdown;
this.set('markdown', markdown);
// use internal values to avoid updating bound values
if (!isEmpty(this.get('isFullScreen'))) {
this.set('_isFullScreen', this.get('isFullScreen'));
}
if (!isEmpty(this.get('isSplitScreen'))) {
this.set('_isSplitScreen', this.get('isSplitScreen'));
}
this._updateButtonState();
},
didInsertElement() {
this._super(...arguments);
this.registerShortcuts();
},
_insertImages(urls) {
let cm = this._editor.codemirror;
// loop through urls and generate image markdown
let images = urls.map((url) => {
// plain url string, so extract filename from path
if (typeOf(url) === 'string') {
let filename = url.split('/').pop();
let alt = filename;
// if we have a normal filename.ext, set alt to filename -ext
if (filename.lastIndexOf('.') > 0) {
alt = filename.slice(0, filename.lastIndexOf('.'));
}
return `![${alt}](${url})`;
// full url object, use attrs we're given
} else {
let image = `![${url.alt}](${url.url})`;
if (url.credit) {
image += `\n${url.credit}`;
}
return image;
}
});
let text = images.join('\n\n');
// clicking the image toolbar button will lose the selection so we use
// the captured selection to re-select here
if (this._imageInsertSelection) {
// we want to focus but not re-position
this.send('focusEditor', null);
// re-select and clear the captured selection so drag/drop still
// inserts at the correct place
cm.setSelection(
this._imageInsertSelection.anchor,
this._imageInsertSelection.head
);
this._imageInsertSelection = null;
}
// focus editor and place cursor at end if not already focused
if (!cm.hasFocus()) {
this.send('focusEditor');
text = `\n\n${text}\n\n`;
}
// insert at cursor or replace selection then position cursor at end
// of inserted text
cm.replaceSelection(text, 'end');
},
// mark the split-pane/full-screen/spellcheck buttons active when they're active
_updateButtonState() {
if (this._editor) {
let sideBySideButton = this._editor.toolbarElements['side-by-side'];
let spellcheckButton = this._editor.toolbarElements.spellcheck;
let hemmingwayButton = this._editor.toolbarElements.hemmingway;
if (sideBySideButton) {
if (this.get('_isSplitScreen')) {
sideBySideButton.classList.add('active');
} else {
sideBySideButton.classList.remove('active');
}
}
if (spellcheckButton) {
if (this._editor.codemirror.getOption('mode') === 'spell-checker') {
spellcheckButton.classList.add('active');
} else {
spellcheckButton.classList.remove('active');
}
}
if (hemmingwayButton) {
if (this._isHemmingwayMode) {
hemmingwayButton.classList.add('active');
} else {
hemmingwayButton.classList.remove('active');
}
}
}
},
// set up the preview auto-update and scroll sync
_connectSplitPreview() {
let cm = this._editor.codemirror;
let editor = this._editor;
/* eslint-disable ember-suave/prefer-destructuring */
let editorPane = this.$('.gh-markdown-editor-pane')[0];
let previewPane = this.$('.gh-markdown-editor-preview')[0];
let previewContent = this.$('.gh-markdown-editor-preview-content')[0];
/* eslint-enable ember-suave/prefer-destructuring */
this._editorPane = editorPane;
this._previewPane = previewPane;
this._previewContent = previewContent;
// from SimpleMDE -------
let sideBySideRenderingFunction = function() {
previewContent.innerHTML = editor.options.previewRender(
editor.value(),
previewContent
);
};
cm.sideBySideRenderingFunction = sideBySideRenderingFunction;
sideBySideRenderingFunction();
cm.on('update', cm.sideBySideRenderingFunction);
// Refresh to fix selection being off (#309)
cm.refresh();
// ----------------------
this._onEditorPaneScroll = this._scrollHandler.bind(this);
editorPane.addEventListener('scroll', this._onEditorPaneScroll, false);
this._scrollSync();
},
_scrollHandler() {
if (!this._scrollSyncTicking) {
requestAnimationFrame(this._scrollSync.bind(this));
}
this._scrollSyncTicking = true;
},
_scrollSync() {
let editorPane = this._editorPane;
let previewPane = this._previewPane;
let height = editorPane.scrollHeight - editorPane.clientHeight;
let ratio = parseFloat(editorPane.scrollTop) / height;
let move = (previewPane.scrollHeight - previewPane.clientHeight) * ratio;
previewPane.scrollTop = move;
this._scrollSyncTicking = false;
},
_disconnectSplitPreview() {
let cm = this._editor.codemirror;
cm.off('update', cm.sideBySideRenderingFunction);
cm.refresh();
this._editorPane.removeEventListener('scroll', this._onEditorPaneScroll, false);
delete this._previewPane;
delete this._previewPaneContent;
delete this._onEditorPaneScroll;
},
_openImageFileDialog({captureSelection = true} = {}) {
if (captureSelection) {
// capture the current selection before it's lost by clicking the
// file input button
this._imageInsertSelection = {
anchor: this._editor.codemirror.getCursor('anchor'),
head: this._editor.codemirror.getCursor('head')
};
}
// trigger the dialog via gh-file-input, when a file is selected it will
// trigger the onImageFilesSelected closure action
this.$('input[type="file"]').click();
},
// wrap SimpleMDE's built-in preview toggle so that we can trigger a closure
// action that can apply our own classes higher up in the DOM
_togglePreview() {
this.onPreviewToggle(!this._editor.isPreviewActive());
this._editor.togglePreview();
},
_toggleSpellcheck() {
let cm = this._editor.codemirror;
if (cm.getOption('mode') === 'spell-checker') {
cm.setOption('mode', 'gfm');
} else {
cm.setOption('mode', 'spell-checker');
}
this._updateButtonState();
},
_toggleHemmingway() {
let cm = this._editor.codemirror;
let extraKeys = cm.getOption('extraKeys');
let notificationText = '';
this._isHemmingwayMode = !this._isHemmingwayMode;
if (this._isHemmingwayMode) {
notificationText = '<span class="gh-notification-title">Hemingway Mode On:</span> Write now; edit later. Backspace disabled.';
extraKeys.Backspace = function () {};
} else {
notificationText = '<span class="gh-notification-title">Hemingway Mode Off:</span> Normal editing restored.';
delete extraKeys.Backspace;
}
cm.setOption('extraKeys', extraKeys);
this._updateButtonState();
cm.focus();
this.get('notifications').showNotification(
htmlSafe(notificationText),
{key: 'editor.hemmingwaymode'}
);
},
willDestroyElement() {
if (this.get('_isSplitScreen')) {
this._disconnectSplitPreview();
}
this.removeShortcuts();
this._super(...arguments);
},
actions: {
// put the markdown into a new mobiledoc card, trigger external update
updateMarkdown(markdown) {
let mobiledoc = copy(BLANK_DOC, true);
mobiledoc.cards[0][1].markdown = markdown;
this.onChange(mobiledoc);
},
// store a reference to the simplemde editor so that we can handle
// focusing and image uploads
setEditor(editor) {
this._editor = editor;
// disable CodeMirror's drag/drop handling as we want to handle that
// in the parent gh-editor component
this._editor.codemirror.setOption('dragDrop', false);
// default to spellchecker being off
this._editor.codemirror.setOption('mode', 'gfm');
// HACK: move the toolbar & status bar elements outside of the
// editor container so that they can be aligned in fixed positions
let container = this.$().closest('.gh-editor').find('.gh-editor-footer');
this._toolbar = this.$('.editor-toolbar');
this._statusbar = this.$('.editor-statusbar');
this._toolbar.appendTo(container);
this._statusbar.appendTo(container);
this._updateButtonState();
},
// used by the title input when the TAB or ENTER keys are pressed
focusEditor(position = 'bottom') {
this._editor.codemirror.focus();
if (position === 'bottom') {
this._editor.codemirror.execCommand('goDocEnd');
} else if (position === 'top') {
this._editor.codemirror.execCommand('goDocStart');
}
return false;
},
// HACK FIXME (PLEASE):
// - clicking toolbar buttons will cause the editor to lose focus
// - this is painful because we often want to know if the editor has focus
// so that we can insert images and so on in the correct place
// - the blur event will always fire before the button action is triggered 😞
// - to work around this we track focus state manually and set it to false
// after an arbitrary period that's long enough to allow the button action
// to trigger first
// - this _may_ well have unknown issues due to browser differences,
// variations in performance, moon cycles, sun spots, or cosmic rays
// - here be 🐲
// - (please let it work 🙏)
updateFocusState(focused) {
if (focused) {
this._editorFocused = true;
} else {
run.later(this, function () {
this._editorFocused = false;
}, 100);
}
},
openImageFileDialog() {
let captureSelection = this._editor.codemirror.hasFocus();
this._openImageFileDialog({captureSelection});
},
toggleUnsplash() {
if (this.get('_showUnsplash')) {
return this.toggleProperty('_showUnsplash');
}
// capture current selection before it's lost by clicking toolbar btn
if (this._editorFocused) {
this._imageInsertSelection = {
anchor: this._editor.codemirror.getCursor('anchor'),
head: this._editor.codemirror.getCursor('head')
};
}
this.toggleProperty('_showUnsplash');
},
insertUnsplashPhoto(photo) {
let image = {
alt: photo.description || '',
url: photo.urls.regular,
credit: `<small>Photo by [${photo.user.name}](${photo.user.links.html}?utm_source=ghost&utm_medium=referral&utm_campaign=api-credit) / [Unsplash](https://unsplash.com/?utm_source=ghost&utm_medium=referral&utm_campaign=api-credit)</small>`
};
this._insertImages([image]);
},
toggleFullScreen() {
let isFullScreen = !this.get('_isFullScreen');
this.set('_isFullScreen', isFullScreen);
this._updateButtonState();
this.onFullScreenToggle(isFullScreen);
// leave split screen when exiting full screen mode
if (!isFullScreen && this.get('_isSplitScreen')) {
this.send('toggleSplitScreen');
}
},
toggleSplitScreen() {
let isSplitScreen = !this.get('_isSplitScreen');
let previewButton = this._editor.toolbarElements.preview;
this.set('_isSplitScreen', isSplitScreen);
this._updateButtonState();
// set up the preview rendering and scroll sync
// afterRender is needed so that necessary components have been
// added/removed and editor pane length has settled
if (isSplitScreen) {
// disable the normal SimpleMDE preview if it's active
if (this._editor.isPreviewActive()) {
let preview = this._editor.toolbar.find((button) => {
return button.name === 'preview';
});
preview.action(this._editor);
}
if (previewButton) {
previewButton.classList.add('disabled');
}
run.scheduleOnce('afterRender', this, this._connectSplitPreview);
} else {
if (previewButton) {
previewButton.classList.remove('disabled');
}
run.scheduleOnce('afterRender', this, this._disconnectSplitPreview);
}
this.onSplitScreenToggle(isSplitScreen);
// go fullscreen when entering split screen mode
this.send('toggleFullScreen');
},
toggleHemmingway() {
this._toggleHemmingway();
},
// put the toolbar/statusbar elements back so that SimpleMDE doesn't throw
// errors when it tries to remove them
destroyEditor() {
let container = this.$('.gh-markdown-editor-pane');
this._toolbar.appendTo(container);
this._statusbar.appendTo(container);
this._editor = null;
}
}
});