forked from filipproch/medium-editor-element
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedium-editor-element.html
More file actions
180 lines (141 loc) · 4.29 KB
/
Copy pathmedium-editor-element.html
File metadata and controls
180 lines (141 loc) · 4.29 KB
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="medium-editor-script.html">
<link rel="import" href="medium-editor-md-script.html">
<!--
`medium-editor`
Polymer element for https://github.com/yabwe/medium-editor
## Usage
The following shows the usage of the element with it's properties
```html
<medium-editor
id="myEditor"
value="{{value}}">
</medium-editor>
<pre>[[value]]</pre>
```
by default, it should just work. It will output HTML using it's value property. If you want markdown output, just change the output property.
```html
<medium-editor
id="myEditor"
output="markdown"
value="{{value}}">
</medium-editor>
<pre>[[value]]</pre>
```
## Style
The preferred approach to styling the element is using CSS custom properties and mixins.
Custom property | Description | Default
:--- | :--- | :---
`--medium-editor-min-height` | The minimum height of the editor. | 300px
@demo demo/index.html
-->
<dom-module id="medium-editor">
<template>
<link rel="stylesheet" href="../medium-editor/dist/css/medium-editor.css">
<link rel="stylesheet" href="../medium-editor/dist/css/themes/default.css">
<style>
:host {
display: block;
width: 100%;
--medium-editor-min-height: 300px;
}
#editor {
width: 100%;
height: 100%;
outline: 0 solid transparent;
min-height: var(--medium-editor-min-height);
}
#editor:focus {
outline: 0 solid transparent;
}
</style>
<div id="editor"></div>
</template>
<script>
Polymer({
is: 'medium-editor',
properties: {
/**
* Instance of MediumEditor class
*/
editor: {
type: Object,
readOnly: true
},
/**
* Custom placeholder text shown in the editor window
*/
placeholderText: {
type: String,
value: null
},
/**
* Defines whether to hide the placeholder upon clicking the text editor
*/
placeholderHide: {
type: Boolean,
value: true
},
/**
* The output format for the text editor
*
* Supported values are 'html' and 'markdown'
*/
output: {
type: String,
value: 'html'
},
/**
* The current content of the text editor in format defined by output property
*/
value: {
type: String,
notify: true,
readOnly: true
}
},
ready: function () {
this._reinitializeEditor()
},
_reinitializeEditor: function () {
if (this.editor) {
this.editor.destroy()
this._setEditor(null)
}
this._setEditor(new MediumEditor(this.$.editor, this._buildOptions()))
this.editor.subscribe('editableInput', this._handleEditableInput.bind(this))
},
_buildOptions: function () {
var options = {}
if (this.placeholderText || this.placeholderHide) {
var placeholderOptions = {}
if (this.placeholderText) {
placeholderOptions[ 'text' ] = this.placeholderText
}
if (this.placeholderHide) {
placeholderOptions[ 'hideOnClick' ] = this.placeholderHide
}
options[ 'placeholder' ] = placeholderOptions
}
var extensions = options[ 'extensions' ] = {}
switch (this.output) {
case 'markdown': {
extensions[ 'markdown' ] = new MeMarkdown(function (markdown) {
this._handleOutput(markdown)
}.bind(this))
}
}
return options
},
_handleEditableInput: function (event, editable) {
if (this.output === 'html') {
this._handleOutput(editable.textContent)
}
},
_handleOutput: function (newValue) {
console.log('_handleOutput', newValue)
this._setValue(newValue)
}
});
</script>
</dom-module>