@contentful/field-editor-markdown 1.2.0 → 1.3.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.
Files changed (136) hide show
  1. package/dist/cjs/MarkdownActions.js +235 -0
  2. package/dist/cjs/MarkdownEditor.js +180 -0
  3. package/dist/cjs/__fixtures__/FakeSdk.js +183 -0
  4. package/dist/cjs/__fixtures__/asset/index.js +37 -0
  5. package/dist/cjs/__fixtures__/content-type/index.js +16 -0
  6. package/dist/cjs/__fixtures__/entry/index.js +33 -0
  7. package/dist/cjs/__fixtures__/fixtures.js +71 -0
  8. package/dist/cjs/__fixtures__/locale/index.js +40 -0
  9. package/dist/cjs/__fixtures__/space/index.js +16 -0
  10. package/dist/cjs/codemirrorImports.js +9 -0
  11. package/dist/cjs/components/HeadingSelector.js +66 -0
  12. package/dist/cjs/components/InsertLinkSelector.js +86 -0
  13. package/dist/cjs/components/MarkdownBottomBar.js +111 -0
  14. package/dist/cjs/components/MarkdownConstraints.js +79 -0
  15. package/dist/cjs/components/MarkdownPreview.js +249 -0
  16. package/dist/cjs/components/MarkdownTabs.js +128 -0
  17. package/dist/cjs/components/MarkdownTextarea/CodeMirrorWrapper.js +383 -0
  18. package/dist/cjs/components/MarkdownTextarea/MarkdownCommands.js +233 -0
  19. package/dist/cjs/components/MarkdownTextarea/MarkdownTextarea.js +190 -0
  20. package/dist/cjs/components/MarkdownTextarea/createMarkdownEditor.js +101 -0
  21. package/dist/cjs/components/MarkdownToolbar.js +367 -0
  22. package/dist/cjs/components/icons.js +193 -0
  23. package/dist/cjs/dialogs/CheatsheetModalDialog.js +239 -0
  24. package/dist/cjs/dialogs/ConfirmInsertAssetModalDialog.js +94 -0
  25. package/dist/cjs/dialogs/EmdebExternalContentDialog.js +202 -0
  26. package/dist/cjs/dialogs/InsertLinkModalDialog.js +149 -0
  27. package/dist/cjs/dialogs/InsertTableModalDialog.js +140 -0
  28. package/dist/cjs/dialogs/SpecialCharacterModalDialog.js +146 -0
  29. package/dist/cjs/dialogs/ZenModeModalDialog.js +257 -0
  30. package/dist/cjs/dialogs/openMarkdownDialog.js +121 -0
  31. package/dist/cjs/dialogs/renderMarkdownDialog.js +108 -0
  32. package/dist/cjs/index.js +29 -0
  33. package/dist/cjs/types.js +20 -0
  34. package/dist/cjs/utils/insertAssetLinks.js +122 -0
  35. package/dist/cjs/utils/insertAssetLinks.spec.js +22 -0
  36. package/dist/cjs/utils/isValidUrl.js +22 -0
  37. package/dist/cjs/utils/linkOrganizer.js +187 -0
  38. package/dist/cjs/utils/linkOrganizer.spec.js +96 -0
  39. package/dist/cjs/utils/replaceMailtoAmp.js +15 -0
  40. package/dist/cjs/utils/replaceMailtoAmp.spec.js +22 -0
  41. package/dist/cjs/utils/specialCharacters.js +228 -0
  42. package/dist/cjs/utils/userAgent.js +28 -0
  43. package/dist/esm/MarkdownActions.js +186 -0
  44. package/dist/esm/MarkdownEditor.js +118 -0
  45. package/dist/esm/__fixtures__/FakeSdk.js +173 -0
  46. package/dist/esm/__fixtures__/asset/index.js +6 -0
  47. package/dist/esm/__fixtures__/content-type/index.js +2 -0
  48. package/dist/esm/__fixtures__/entry/index.js +5 -0
  49. package/dist/esm/__fixtures__/fixtures.js +6 -0
  50. package/dist/esm/__fixtures__/locale/index.js +15 -0
  51. package/dist/esm/__fixtures__/space/index.js +2 -0
  52. package/dist/esm/codemirrorImports.js +5 -0
  53. package/dist/esm/components/HeadingSelector.js +17 -0
  54. package/dist/esm/components/InsertLinkSelector.js +37 -0
  55. package/dist/esm/components/MarkdownBottomBar.js +46 -0
  56. package/dist/esm/components/MarkdownConstraints.js +25 -0
  57. package/dist/esm/components/MarkdownPreview.js +195 -0
  58. package/dist/esm/components/MarkdownTabs.js +74 -0
  59. package/dist/esm/components/MarkdownTextarea/CodeMirrorWrapper.js +329 -0
  60. package/dist/esm/components/MarkdownTextarea/MarkdownCommands.js +218 -0
  61. package/dist/esm/components/MarkdownTextarea/MarkdownTextarea.js +136 -0
  62. package/dist/esm/components/MarkdownTextarea/createMarkdownEditor.js +52 -0
  63. package/dist/esm/components/MarkdownToolbar.js +302 -0
  64. package/dist/esm/components/icons.js +112 -0
  65. package/dist/esm/dialogs/CheatsheetModalDialog.js +177 -0
  66. package/dist/esm/dialogs/ConfirmInsertAssetModalDialog.js +37 -0
  67. package/dist/esm/dialogs/EmdebExternalContentDialog.js +140 -0
  68. package/dist/esm/dialogs/InsertLinkModalDialog.js +92 -0
  69. package/dist/esm/dialogs/InsertTableModalDialog.js +78 -0
  70. package/dist/esm/dialogs/SpecialCharacterModalDialog.js +84 -0
  71. package/dist/esm/dialogs/ZenModeModalDialog.js +195 -0
  72. package/dist/esm/dialogs/openMarkdownDialog.js +72 -0
  73. package/dist/esm/dialogs/renderMarkdownDialog.js +59 -0
  74. package/dist/esm/index.js +5 -0
  75. package/dist/esm/types.js +10 -0
  76. package/dist/esm/utils/insertAssetLinks.js +99 -0
  77. package/dist/esm/utils/insertAssetLinks.spec.js +18 -0
  78. package/dist/esm/utils/isValidUrl.js +4 -0
  79. package/dist/esm/utils/linkOrganizer.js +152 -0
  80. package/dist/esm/utils/linkOrganizer.spec.js +53 -0
  81. package/dist/esm/utils/replaceMailtoAmp.js +5 -0
  82. package/dist/esm/utils/replaceMailtoAmp.spec.js +18 -0
  83. package/dist/esm/utils/specialCharacters.js +218 -0
  84. package/dist/esm/utils/userAgent.js +13 -0
  85. package/dist/{MarkdownActions.d.ts → types/MarkdownActions.d.ts} +38 -38
  86. package/dist/{MarkdownEditor.d.ts → types/MarkdownEditor.d.ts} +22 -22
  87. package/dist/types/__fixtures__/FakeSdk.d.ts +8 -0
  88. package/dist/types/__fixtures__/asset/index.d.ts +6 -0
  89. package/dist/types/__fixtures__/content-type/index.d.ts +2 -0
  90. package/dist/types/__fixtures__/entry/index.d.ts +5 -0
  91. package/dist/types/__fixtures__/fixtures.d.ts +6 -0
  92. package/dist/types/__fixtures__/locale/index.d.ts +42 -0
  93. package/dist/types/__fixtures__/space/index.d.ts +2 -0
  94. package/dist/{codemirrorImports.d.ts → types/codemirrorImports.d.ts} +5 -5
  95. package/dist/{components → types/components}/HeadingSelector.d.ts +7 -7
  96. package/dist/{components → types/components}/InsertLinkSelector.d.ts +9 -9
  97. package/dist/{components → types/components}/MarkdownBottomBar.d.ts +11 -11
  98. package/dist/{components → types/components}/MarkdownConstraints.d.ts +6 -6
  99. package/dist/{components → types/components}/MarkdownPreview.d.ts +14 -14
  100. package/dist/{components → types/components}/MarkdownTabs.d.ts +8 -8
  101. package/dist/{components → types/components}/MarkdownTextarea/CodeMirrorWrapper.d.ts +58 -58
  102. package/dist/{components → types/components}/MarkdownTextarea/MarkdownCommands.d.ts +33 -33
  103. package/dist/{components → types/components}/MarkdownTextarea/MarkdownTextarea.d.ts +17 -17
  104. package/dist/{components → types/components}/MarkdownTextarea/createMarkdownEditor.d.ts +55 -55
  105. package/dist/{components → types/components}/MarkdownToolbar.d.ts +12 -12
  106. package/dist/types/components/icons.d.ts +18 -0
  107. package/dist/{dialogs → types/dialogs}/CheatsheetModalDialog.d.ts +4 -4
  108. package/dist/{dialogs → types/dialogs}/ConfirmInsertAssetModalDialog.d.ts +23 -23
  109. package/dist/{dialogs → types/dialogs}/EmdebExternalContentDialog.d.ts +9 -9
  110. package/dist/{dialogs → types/dialogs}/InsertLinkModalDialog.d.ts +17 -17
  111. package/dist/{dialogs → types/dialogs}/InsertTableModalDialog.d.ts +13 -13
  112. package/dist/{dialogs → types/dialogs}/SpecialCharacterModalDialog.d.ts +9 -9
  113. package/dist/{dialogs → types/dialogs}/ZenModeModalDialog.d.ts +24 -24
  114. package/dist/{dialogs → types/dialogs}/openMarkdownDialog.d.ts +5 -5
  115. package/dist/{dialogs → types/dialogs}/renderMarkdownDialog.d.ts +8 -8
  116. package/dist/{index.d.ts → types/index.d.ts} +5 -5
  117. package/dist/{types.d.ts → types/types.d.ts} +75 -75
  118. package/dist/{utils → types/utils}/insertAssetLinks.d.ts +29 -29
  119. package/dist/types/utils/insertAssetLinks.spec.d.ts +1 -0
  120. package/dist/{utils → types/utils}/isValidUrl.d.ts +2 -2
  121. package/dist/{utils → types/utils}/linkOrganizer.d.ts +6 -6
  122. package/dist/types/utils/linkOrganizer.spec.d.ts +1 -0
  123. package/dist/{utils → types/utils}/replaceMailtoAmp.d.ts +1 -1
  124. package/dist/types/utils/replaceMailtoAmp.spec.d.ts +1 -0
  125. package/dist/{utils → types/utils}/specialCharacters.d.ts +4 -4
  126. package/dist/{utils → types/utils}/userAgent.d.ts +1 -1
  127. package/package.json +25 -11
  128. package/CHANGELOG.md +0 -308
  129. package/dist/components/icons.d.ts +0 -18
  130. package/dist/field-editor-markdown.cjs.development.js +0 -3605
  131. package/dist/field-editor-markdown.cjs.development.js.map +0 -1
  132. package/dist/field-editor-markdown.cjs.production.min.js +0 -216
  133. package/dist/field-editor-markdown.cjs.production.min.js.map +0 -1
  134. package/dist/field-editor-markdown.esm.js +0 -3595
  135. package/dist/field-editor-markdown.esm.js.map +0 -1
  136. package/dist/index.js +0 -8
@@ -0,0 +1,218 @@
1
+ import max from 'lodash/max';
2
+ import min from 'lodash/min';
3
+ import range from 'lodash/range';
4
+ import times from 'lodash/times';
5
+ import repeat from 'lodash/repeat';
6
+ function createPrefixToggleFn(prefix) {
7
+ return (editor)=>{
8
+ if (editor.lineStartsWith(prefix)) {
9
+ editor.removeFromLineBeginning(prefix.length);
10
+ } else {
11
+ editor.insertAtLineBeginning(prefix);
12
+ }
13
+ };
14
+ }
15
+ function wrapSelection(editor, marker, emptyText) {
16
+ return ()=>{
17
+ editor.usePrimarySelection();
18
+ if (editor.getSelection()) {
19
+ const selectedText = editor.getSelectedText();
20
+ if (selectedText.startsWith(marker) && selectedText.endsWith(marker)) {
21
+ const markerLength = marker.length;
22
+ const textWithoutMarker = selectedText.slice(markerLength, selectedText.length - markerLength);
23
+ editor.replaceSelectedText(textWithoutMarker);
24
+ } else {
25
+ editor.wrapSelection(marker);
26
+ }
27
+ } else {
28
+ editor.insertAtCursor(marker + emptyText + marker);
29
+ editor.selectBackwards(marker.length, emptyText.length);
30
+ }
31
+ };
32
+ }
33
+ const HEADER_CHAR = '#';
34
+ const quoteToggleFn = createPrefixToggleFn('> ');
35
+ const codeToggleFn = createPrefixToggleFn(' ');
36
+ export function create(editor) {
37
+ return {
38
+ bold: wrapSelection(editor, '__', 'text in bold'),
39
+ italic: wrapSelection(editor, '*', 'text in italic'),
40
+ strike: wrapSelection(editor, '~~', 'striked out'),
41
+ quote: modifySelection(editor, quoteToggleFn),
42
+ code: modifySelection(editor, codeToggleFn),
43
+ link,
44
+ h1: toggleHeader(editor, 1),
45
+ h2: toggleHeader(editor, 2),
46
+ h3: toggleHeader(editor, 3),
47
+ ul: modifySelection(editor, ulToggleFn, true),
48
+ ol: modifySelection(editor, olToggleFn, true),
49
+ undo: function() {
50
+ editor.cmd('undo');
51
+ },
52
+ redo: function() {
53
+ editor.cmd('redo');
54
+ },
55
+ hr,
56
+ indent,
57
+ dedent,
58
+ table
59
+ };
60
+ function hr() {
61
+ editor.moveIfNotEmpty();
62
+ const nl = editor.getNl();
63
+ const markup = nl + '---' + nl + nl;
64
+ editor.insertAtCursor(markup);
65
+ }
66
+ function indent() {
67
+ editor.insertAtLineBeginning(editor.getIndentation());
68
+ }
69
+ function dedent() {
70
+ const indentation = editor.getIndentation();
71
+ if (editor.lineStartsWith(indentation)) {
72
+ editor.removeFromLineBeginning(indentation.length);
73
+ }
74
+ }
75
+ function table(config) {
76
+ const nl = editor.getNl();
77
+ editor.moveIfNotEmpty();
78
+ editor.insertAtCursor(nl);
79
+ const line = editor.getCurrentLineNumber();
80
+ editor.insertAtCursor(tableTemplate(config.rows, config.cols).join(nl));
81
+ editor.insertAtCursor(nl + nl);
82
+ editor.restoreCursor(2, line);
83
+ }
84
+ function link(url, text, title) {
85
+ editor.usePrimarySelection();
86
+ const linkTitle = title ? ' "' + title + '"' : '';
87
+ const link = text ? '[' + text + '](' + url + linkTitle + ')' : '<' + url + '>';
88
+ editor.replaceSelectedText(link, 'around');
89
+ }
90
+ }
91
+ function modifySelection(editor, toggleFn, isList) {
92
+ return ()=>{
93
+ editor.usePrimarySelection();
94
+ const selection = editor.getSelection();
95
+ if (selection) {
96
+ forLineIn(selection, (lineNumber, listNumber)=>{
97
+ editor.moveToLineBeginning(lineNumber);
98
+ toggleFn(editor, listNumber);
99
+ });
100
+ editor.moveToLineEnd();
101
+ } else {
102
+ if (isList && !getListNumber(editor) && !editor.lineStartsWith('- ')) {
103
+ prepareListWhitespace(editor);
104
+ }
105
+ toggleFn(editor);
106
+ }
107
+ };
108
+ }
109
+ function forLineIn(selection, cb) {
110
+ const lines = [
111
+ selection.anchor.line,
112
+ selection.head.line
113
+ ];
114
+ const maxNumber = max(lines);
115
+ const minNumber = min(lines);
116
+ const lineRange = range(minNumber || 0, maxNumber !== undefined ? maxNumber + 1 : undefined);
117
+ lineRange.forEach((lineNumber, i)=>{
118
+ cb(lineNumber, i + 1);
119
+ });
120
+ }
121
+ function prepareListWhitespace(editor) {
122
+ const line = editor.getCurrentLineNumber();
123
+ const isCurrentLineEmpty = editor.isLineEmpty(line);
124
+ const isPrevLineEmpty = line > 0 ? editor.isLineEmpty(line - 1) : false;
125
+ const isNextLineEmpty = line < editor.getLinesCount() - 1 ? editor.isLineEmpty(line + 1) : true;
126
+ let linesToInsert = isCurrentLineEmpty ? 2 : 4;
127
+ if (isPrevLineEmpty) {
128
+ linesToInsert = linesToInsert - 1;
129
+ }
130
+ if (isNextLineEmpty) {
131
+ linesToInsert = linesToInsert - 1;
132
+ }
133
+ editor.moveToLineEnd();
134
+ editor.insertAtCursor(editor.getNl(linesToInsert));
135
+ editor.restoreCursor(0, isCurrentLineEmpty ? line : line + 2);
136
+ }
137
+ function getListNumber(editor) {
138
+ const result = editor.getCurrentLine().match(/^(\d+\. )/);
139
+ return result ? result[1] : null;
140
+ }
141
+ function ulToggleFn(editor) {
142
+ if (editor.lineStartsWith('- ')) {
143
+ editor.removeFromLineBeginning(2);
144
+ } else {
145
+ const listNumber = getListNumber(editor);
146
+ if (listNumber) {
147
+ editor.removeFromLineBeginning(listNumber.length);
148
+ }
149
+ editor.insertAtLineBeginning('- ');
150
+ }
151
+ }
152
+ function olToggleFn(editor, n) {
153
+ const listNumber = getListNumber(editor);
154
+ if (listNumber) {
155
+ editor.removeFromLineBeginning(listNumber.length);
156
+ } else {
157
+ if (editor.lineStartsWith('- ')) {
158
+ editor.removeFromLineBeginning(2);
159
+ }
160
+ editor.insertAtLineBeginning((n || 1) + '. ');
161
+ }
162
+ }
163
+ function tableTemplate(nrows, ncols) {
164
+ const cellWidth = new Array(11);
165
+ const cell = ' ' + cellWidth.join(' ') + ' |';
166
+ const separatorCell = ' ' + cellWidth.join('-') + ' |';
167
+ let baseRow = '|';
168
+ let separatorRow = '|';
169
+ times(ncols, ()=>{
170
+ baseRow += cell;
171
+ separatorRow += separatorCell;
172
+ });
173
+ const bodyRows = range(nrows).map(()=>baseRow.replace(/\| {5}/g, '| Cell'));
174
+ const headerRow = baseRow.replace(/\| {7}/g, '| Header');
175
+ return [
176
+ headerRow,
177
+ separatorRow
178
+ ].concat(bodyRows);
179
+ }
180
+ function toggleHeader(editor, level) {
181
+ return ()=>{
182
+ const initialCh = editor.getCurrentCharacter();
183
+ const currentHeader = selectHeader(editor);
184
+ const prefix = repeat(HEADER_CHAR, level);
185
+ if (!currentHeader) {
186
+ editor.moveToLineBeginning();
187
+ editor.insertAtCursor(prefix + ' ');
188
+ editor.restoreCursor(initialCh + prefix.length + 1);
189
+ return;
190
+ }
191
+ if (editor.getSelectedText() === prefix) {
192
+ editor.extendSelectionBy(1);
193
+ const removedCh = editor.getSelectionLength();
194
+ editor.removeSelectedText();
195
+ editor.restoreCursor(initialCh - removedCh);
196
+ return;
197
+ }
198
+ const diff = prefix.length - editor.getSelectionLength();
199
+ editor.replaceSelectedText(prefix);
200
+ editor.restoreCursor(initialCh + diff);
201
+ };
202
+ }
203
+ function selectHeader(editor) {
204
+ const result = editor.getCurrentLine().match(/^( {0,3})(#{1,6}) /);
205
+ if (!result) {
206
+ return null;
207
+ }
208
+ const indentation = result[1];
209
+ const header = result[2];
210
+ editor.select(getPos(0), getPos(header.length));
211
+ return editor.getSelection();
212
+ function getPos(modifier) {
213
+ return {
214
+ line: editor.getCurrentLineNumber(),
215
+ ch: indentation.length + modifier
216
+ };
217
+ }
218
+ }
@@ -0,0 +1,136 @@
1
+ import React, { useRef, useEffect, useState } from 'react';
2
+ import tokens from '@contentful/f36-tokens';
3
+ import { css, cx } from 'emotion';
4
+ import { createMarkdownEditor } from './createMarkdownEditor';
5
+ const styles = {
6
+ root: css`
7
+ border: 1px solid ${tokens.gray400};
8
+ border-width: 0 1px;
9
+ overflow-y: auto;
10
+ height: auto;
11
+ min-height: 300px;
12
+
13
+ .CodeMirror {
14
+ height: auto;
15
+ line-height: ${tokens.lineHeightDefault};
16
+ }
17
+ .CodeMirror-lines {
18
+ color: ${tokens.gray700};
19
+ padding: ${tokens.spacingL};
20
+ }
21
+ .CodeMirror-code {
22
+ font-family: ${tokens.fontStackMonospace};
23
+ font-size: ${tokens.fontSizeM};
24
+ }
25
+ .CodeMirror-scroll {
26
+ min-height: '6rem';
27
+ }
28
+
29
+ .cm-header {
30
+ color: ${tokens.gray900};
31
+ }
32
+ span.cm-variable-2 {
33
+ color: ${tokens.gray700};
34
+ }
35
+ .cm-header-1 {
36
+ font-size: 1.9em;
37
+ }
38
+ .cm-header-2 {
39
+ font-size: 1.75em;
40
+ }
41
+ .cm-header-3 {
42
+ font-size: 1.6em;
43
+ }
44
+ .cm-header-4 {
45
+ font-size: 1.45em;
46
+ }
47
+ .cm-header-5 {
48
+ font-size: 1.3em;
49
+ }
50
+ .cm-header-6 {
51
+ font-size: 1.15em;
52
+ }
53
+
54
+ span.cm-tag,
55
+ span.cm-string,
56
+ span.cm-attribute {
57
+ color: ${tokens.red600};
58
+ }
59
+ span.cm-string {
60
+ text-decoration: none !important;
61
+ }
62
+ span.cm-quote,
63
+ span.cm-comment {
64
+ color: ${tokens.gray600};
65
+ }
66
+ span.cm-link,
67
+ span.cm-url {
68
+ color: ${tokens.blue500} !important;
69
+ }
70
+ span.cm-link {
71
+ text-decoration: underline;
72
+ }
73
+ `,
74
+ framed: css({
75
+ '.CodeMirror': {
76
+ maxHeight: '500px'
77
+ }
78
+ }),
79
+ zen: css({
80
+ border: 'none !important',
81
+ '.CodeMirror-lines': {
82
+ maxWidth: '650px',
83
+ margin: '0 auto'
84
+ }
85
+ }),
86
+ disabled: css`
87
+ .CodeMirror {
88
+ background: ${tokens.gray100};
89
+ cursor: 'not-allowed';
90
+ }
91
+ .CodeMirror-cursors {
92
+ visibility: hidden !important;
93
+ }
94
+ .CodeMirror-scroll,
95
+ .CodeMirror-sizer,
96
+ .CodeMirror-lines {
97
+ cursor: not-allowed;
98
+ }
99
+ `
100
+ };
101
+ export const MarkdownTextarea = React.memo((props)=>{
102
+ const hostRef = useRef(null);
103
+ const [editor, setEditor] = useState(null);
104
+ useEffect(()=>{
105
+ if (hostRef.current) {
106
+ setEditor(createMarkdownEditor(hostRef.current, Object.assign({}, {
107
+ direction: props.direction,
108
+ readOnly: true
109
+ }, props.mode === 'zen' ? {
110
+ fixedHeight: true,
111
+ height: '100%'
112
+ } : {}, props.minHeight !== undefined ? {
113
+ height: props.minHeight
114
+ } : {})));
115
+ }
116
+ }, []);
117
+ useEffect(()=>{
118
+ if (editor) {
119
+ props.onReady(editor);
120
+ }
121
+ }, [
122
+ editor
123
+ ]);
124
+ const className = cx(styles.root, props.minHeight !== undefined ? css({
125
+ minHeight: props.minHeight
126
+ }) : undefined, props.mode === 'default' ? styles.framed : styles.zen, props.disabled && styles.disabled);
127
+ return React.createElement("div", {
128
+ className: className,
129
+ ref: hostRef,
130
+ "data-test-id": "markdown-textarea",
131
+ style: {
132
+ display: props.visible ? 'block' : 'none'
133
+ }
134
+ });
135
+ });
136
+ MarkdownTextarea.displayName = 'MarkdownTextarea';
@@ -0,0 +1,52 @@
1
+ import * as CodeMirrorWrapper from './CodeMirrorWrapper';
2
+ import * as Commands from './MarkdownCommands';
3
+ export function createMarkdownEditor(host, options) {
4
+ const editor = CodeMirrorWrapper.create(host, options);
5
+ function wrapChange(fn) {
6
+ return (e, ch)=>{
7
+ fn(editor.getValue(), e, ch);
8
+ };
9
+ }
10
+ const api = {
11
+ actions: Commands.create(editor),
12
+ history: {
13
+ hasUndo: function() {
14
+ return editor.getHistorySize('undo') > 0;
15
+ },
16
+ hasRedo: function() {
17
+ return editor.getHistorySize('redo') > 0;
18
+ }
19
+ },
20
+ events: {
21
+ onScroll: function(fn) {
22
+ editor.attachEvent('scroll', fn, 150);
23
+ },
24
+ onChange: function(fn) {
25
+ editor.attachEvent('change', wrapChange(fn), 0);
26
+ },
27
+ onPaste: function(fn) {
28
+ editor.attachEvent('paste', fn, 0);
29
+ }
30
+ },
31
+ insert: editor.insertAtCursor,
32
+ focus: editor.focus,
33
+ getContent: editor.getValue,
34
+ destroy: editor.destroy,
35
+ setContent: editor.setValue,
36
+ getSelectedText: editor.getSelectedText,
37
+ usePrimarySelection: editor.usePrimarySelection,
38
+ setReadOnly: editor.setReadOnly,
39
+ selectBackwards: editor.selectBackwards,
40
+ getCursor: editor.getCursor,
41
+ setCursor: editor.setCursor,
42
+ clear: ()=>editor.setValue(''),
43
+ selectAll: editor.selectAll,
44
+ setFullsize: editor.setFullsize,
45
+ refresh: editor.refresh
46
+ };
47
+ editor.addKeyShortcuts({
48
+ B: api.actions.bold,
49
+ I: api.actions.italic
50
+ });
51
+ return api;
52
+ }