|
@@ -73,3 +73,49 @@ if (previewButton) {
|
73
|
73
|
);
|
74
|
74
|
});
|
75
|
75
|
}
|
|
76
|
+
|
|
77
|
+document.querySelectorAll('[data-editor-helper]').forEach((helperBtn) => {
|
|
78
|
+ helperBtn.addEventListener('click', (e) => {
|
|
79
|
+ e.preventDefault();
|
|
80
|
+
|
|
81
|
+ const textarea = document.querySelector('[name=body]');
|
|
82
|
+ const currentValue = textarea.value;
|
|
83
|
+
|
|
84
|
+ const start = textarea.selectionStart;
|
|
85
|
+ const end = textarea.selectionEnd;
|
|
86
|
+
|
|
87
|
+ const leftChunk = currentValue.substring(0, start);
|
|
88
|
+ const rightChunk = currentValue.substring(end, textarea.value.length);
|
|
89
|
+
|
|
90
|
+ let insertion = '';
|
|
91
|
+ let startOffset = 0;
|
|
92
|
+ let endOffset = 0;
|
|
93
|
+ switch (e.target.dataset.editorHelper) {
|
|
94
|
+ case 'bold':
|
|
95
|
+ insertion = '****';
|
|
96
|
+ startOffset = 2;
|
|
97
|
+ endOffset = 2;
|
|
98
|
+ break;
|
|
99
|
+ case 'italic':
|
|
100
|
+ insertion = '__';
|
|
101
|
+ startOffset = 1;
|
|
102
|
+ endOffset = 1;
|
|
103
|
+ break;
|
|
104
|
+ case 'link':
|
|
105
|
+ insertion = '[text](url)';
|
|
106
|
+ startOffset = 1;
|
|
107
|
+ endOffset = 5;
|
|
108
|
+ break;
|
|
109
|
+ case 'code':
|
|
110
|
+ insertion = '```js\n```'
|
|
111
|
+ startOffset = 3;
|
|
112
|
+ endOffset = 5;
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ textarea.value = leftChunk + insertion + rightChunk;
|
|
116
|
+
|
|
117
|
+ textarea.focus();
|
|
118
|
+ textarea.selectionStart = start + startOffset;
|
|
119
|
+ textarea.selectionEnd = end + endOffset;
|
|
120
|
+ })
|
|
121
|
+});
|