This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown-editor.html
218 lines (182 loc) · 5.42 KB
/
markdown-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
<link rel="import" href="../polymer/polymer.html">
<!-- Paper elements -->
<link rel="import" href="../paper-input/paper-input-behavior.html">
<link rel="import" href="../paper-input/paper-input-container.html">
<!-- Iron elements -->
<link rel="import" href="../iron-media-query/iron-media-query.html">
<!-- Molecule elements -->
<link rel="import" href="../marked-element/marked-element.html">
<!-- Custom elements -->
<link rel="import" href="medium-editor-markdown-import.html">
<link rel="import" href="../demo-content/shared-markdown-styles.html">
<!--
`<markdown-editor>` is a Medium.com WYSIWYG editor clone (https://github.com/yabwe/medium-editor#button-options) w/ a Markdown extension (https://github.com/IonicaBizau/medium-editor-markdown)
@demo demo/index.html
-->
<dom-module id="markdown-editor">
<template>
<style include="shared-markdown-styles">
:host {
display: block;
}
#preview {
background-color: #ededed;
overflow-x: scroll;
}
#preview[hidden] {
display: none;
}
</style>
<iron-media-query query="max-width: 600px" query-matches="{{_smallScreen}}"></iron-media-query>
<paper-input-container id="editor"
always-float-label>
<label>description</label>
<marked-element id="marked" class="paper-input-input">
<div id="input" class="markdown-html" is="iron-input"></div>
</marked-element>
</paper-input-container>
<pre id="preview" hidden="[[!preview]]">[[markdown]]</pre>
</template>
<script>
Polymer({
is: 'markdown-editor',
properties: {
/**
* Instance of MediumEditor class
*/
editor: {
type: Object,
readOnly: true,
observer: '_editorChanged'
},
/**
* Custom placeholder text shown in the editor window
*/
placeholderText: {
type: String,
value: null
},
/**
* Hides the placeholder upon clicking the text editor when true
*/
placeholderHide: {
type: Boolean,
value: true
},
/**
* The markdown to be rendered
*/
markdown: {
type: String,
notify: true
},
/**
* Displaying a preview of the markdown when true
*/
preview: {
type: Boolean,
reflectToAttribute: true,
value: false
},
_smallScreen: {
type: Boolean
}
},
ready: function () {
this._initializeEditor();
},
_initializeEditor: function () {
if (this.editor) {
this.editor.destroy();
this._setEditor(null);
}
this._setEditor(new MediumEditor(this.$.input, this._buildOptions()));
// cleaning span tags
this.editor.subscribe('editableInput', this._handleEditableInput.bind(this));
},
_buildOptions: function () {
var options = {};
options['targetBlank'] = true;
// TOOLBAR
var toolbarOptions = {};
toolbarOptions['buttons'] = [
'bold',
'italic',
'anchor',
'h1',
'h2',
'h3',
'quote',
'unorderedlist',
'orderedlist',
'removeFormat'
];
if (this._smallScreen) {
toolbarOptions['static'] = true;
toolbarOptions['sticky'] = true;
}
options['toolbar'] = toolbarOptions;
// PASTE
var pasteOptions = {};
pasteOptions['forcePlainText'] = true;
options['paste'] = pasteOptions;
// PLACEHOLDER
placeholderOptions = {};
if (this.placeholderText) {
if (this.placeholderText) {
placeholderOptions['text'] = this.placeholderText;
}
if (this.placeholderHide) {
placeholderOptions['hideOnClick'] = this.placeholderHide;
}
}
options['placeholder'] = placeholderOptions;
// EXTENSIONS
var extensionOptions = {};
extensionOptions['markdown'] = new MeMarkdown({
events: [
'input',
'change',
'click',
'focus'
]
}, function(markdown) {
this._handleOutput(markdown);
}.bind(this));
options['extensions'] = extensionOptions;
return options;
},
_handleEditableInput: function (event, element) {
// clean those span tags
var spanList = element.getElementsByTagName('span');
for (var i = 0; i < spanList.length; i++) {
spanList[i].outerHTML = spanList[i].innerHTML;
};
},
_handleOutput: function (md) {
if (this._editorReady) {
// console.log('md:\n"%s"', md);
this.set('markdown', md);
}
},
_editorChanged: function(editor) {
if (editor) {
this.renderMarkdown();
this.set('_editorReady', true);
}
},
/**
* Applies the markdown property to `marked-element`
*/
renderMarkdown: function() {
// console.log('renderMarkdown: %s', this.markdown);
if (this.markdown) {
this.editor.trigger('focus', {}, this.$.input);
} else {
this.editor.trigger('blur', {}, this.$.input);
}
this.$.marked.markdown = this.markdown;
}
});
</script>
</dom-module>