-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommonmark-editor.html
263 lines (235 loc) · 7.29 KB
/
commonmark-editor.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="./commonmark-editor-import.html">
<!--
A markdown editor for [commonmark](http://commonmark.org/) inspired by the [commonmark demo](http://spec.commonmark.org/dingus).
## Usage
<commonmark-editor height="500px">
Some initial markdown content (optional)
</commonmark-editor>
## `workingDir` attribute
If it is set, every image url which contain only image name will concatenate with its value. Example if `workingDir = "/content/"` then
* `` will be replaced by ``
* `` will be replaced by ``
* `` won't changed
* `` won't changed
* `` won't changed
## Dirty state
* property `changedByUser` = true (dirty): the value was changed by user
##
TODO: make `workingDir` work with every relative local path
-->
<dom-module id="commonmark-editor">
<template>
<textarea class="half" id="input" placeholder="{{placeholder}}" on-keyup="keyPressed" on-tap="markSelection" value="{{value::input}}"></textarea>
<div class="half" id="preview"></div>
<style>
* {
box-sizing: border-box;
}
textarea {
width: 100%;
height: 100%;
resize: vertical;
}
.half {
float: left;
width: 50%;
height: 400px;
}
::content.selected {
background-color: var(--preview-text-highlight, lightblue);
}
#preview {
padding-left: var(--editor-preview-padding, 5px);
overflow: scroll;
resize: vertical;
}
@media only screen and (max-width: 768px) {
.half {
float: left;
width: 100%;
height: 400px;
}
}
</style>
</template>
<script>
Polymer({
is: 'commonmark-editor',
created: function(){
this.reader = new commonmark.Parser();
this.writer = new commonmark.HtmlRenderer({ sourcepos: true });
},
ready: function() {
//yeild the distribution content to the textarea (to transclude the imperative content)
var content = Polymer.dom(this).textContent;
if (content) {
this.initValue(content);
}
//set height
this.$.input.style.height = this.height;
this.$.preview.style.height = this.height;
},
properties: {
height: {
type: String,
value: "400px"
},
placeholder: {
type: String,
value: 'Markdown content'
},
/**
* if set, every 'name-only' image URL will concatenate with its value
*/
workingDir: String,
value: {
type: String,
observer: "inputChangeAction",
notify: true
},
/**
* Use to detect the dirty input state
* - turn to true when the text is changed by human input
* - reset to false if the initValue(s) method is called
*/
changedByUser: {
type: Boolean,
readOnly: true,
notify: true,
default: false
}
},
/**
* set the value and clear the dirty state
*/
initValue: function(v) {
this.$.input.value = v;
this.inputChangeAction(v);
this._setChangedByUser(false);
//console.info(false);
},
inputChangeAction: function(e) {
var textArea = this.$.input;
this.debounce("inputChangeAction", function(){
//only update preview if the content has been changed
if (this.cachedSrc !== textArea.value) {
this.cachedSrc = textArea.value;
var parsed = this.reader.parse(this.cachedSrc);
//concatenate the workingDir to every "plain image link"
if (this.workingDir) {
parsed = this._fixPlainImageLink(parsed, this._addMissingSlash(this.workingDir));
}
this.$.preview.innerHTML = this.writer.render(parsed);
}
this.markSelection();
}, 500);
},
keyPressed: function(e) {
var textArea = this.$.input;
if (!this.changedByUser) {
this._setChangedByUser(this.cachedSrc !== textArea.value);
//console.info(this.cachedSrc !== textArea.value);
}
this.inputChangeAction(false);
},
markSelection: function() {
var lineNumber = this._getLineToPreview();
var preview = this.$.preview;
//TODO implement a more accurate method to find the nodeToHighlight
var nodeToHighlight = preview.querySelector("*[data-sourcepos^='"+lineNumber+":']");
this._highlightInPreview(nodeToHighlight);
},
/**
* highlight (mark selection) a node in the preview div panel, and scroll it to the center of the preview
* panel
* @param nodeToHighlight: a node in the preview div
*/
_highlightInPreview: function(nodeToHighlight) {
if (this.cacheHighlightedNode === nodeToHighlight) {
return; //the node has already been highlighted
}
if (nodeToHighlight) {
if (this.cacheHighlightedNode) {
this.cacheHighlightedNode.classList.remove("selected");
}
//scroll the node to the center of the preview panel
preview.scrollTop = nodeToHighlight.offsetTop - preview.offsetHeight/2;
//highlight it
nodeToHighlight.classList.add("selected");
this.cacheHighlightedNode = nodeToHighlight;
}
else {
//unhighlight (deselect) the current highlighted node in cache
if (this.cacheHighlightedNode) {
this.cacheHighlightedNode.classList.remove("selected");
delete this.cacheHighlightedNode;
}
}
},
/**
* get the line number of the source to preview
* this function will return the last non-empty line of the source just before the caret in the textarea
*/
_getLineToPreview: function() {
var textArea = this.$.input;
//use the character on the right of the caret
var pos = textArea.selectionStart;
if (pos+1 < textArea.value.length) {
pos++;
}
//contentBeforeCaret is trim every spaces, \n on the right
var contentBeforeCaret = this._trimRightSpacesNewline(textArea.value.substr(0, pos));
var currentLine = this._numberOfLine(contentBeforeCaret);
return currentLine;
},
/**
* count number of line by counting all the returned line characters
* @param s: String
*/
_numberOfLine: function(s) {
return (s.match(/\n/g) || []).length + 1;
},
/**
* trim all spaces and returned line character on the right
* @param s: String
*/
_trimRightSpacesNewline: function(s) {
var pattern = /( |\n)+$/g;
var resu = s.replace(pattern, '');
return resu;
},
/**
* - concatenate the workingDir to every "plain image link" (plain image link is filename only and does not contain any '/'). eg
* -  will be transformed to 
* -  won't change
* @param parsedMarkdown
* @param workingDir: String
*/
_fixPlainImageLink: function(parsedMarkdown, workingDir) {
var walker = parsedMarkdown.walker();
var event, node;
while ((event = walker.next())) {
node = event.node;
if (node.type === 'Image' && event.entering) {
var imageUrl = node.destination;
if (imageUrl.indexOf('/') <= -1) { //if the imageUrl is plain
node.destination = workingDir+imageUrl;
}
}
}
return parsedMarkdown;
},
/**
* _addMissingSlash("foo") returns "foo/"
* @param dir: String
*/
_addMissingSlash: function(dir) {
if (dir.charAt(dir.length-1)!=='/') {
return dir+'/';
}
return dir;
}
});
</script>
</dom-module>