forked from tiff/wysihtml5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.js
179 lines (158 loc) · 6.05 KB
/
editor.js
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
/**
* WYSIHTML5 Editor
*
* @param {Element} textareaElement Reference to the textarea which should be turned into a rich text interface
* @param {Object} [config] See defaultConfig object below for explanation of each individual config option
*
* @events
* load
* beforeload (for internal use only)
* focus
* focus:composer
* focus:textarea
* blur
* blur:composer
* blur:textarea
* change
* change:composer
* change:textarea
* paste
* paste:composer
* paste:textarea
* newword:composer
* destroy:composer
* undo:composer
* redo:composer
* beforecommand:composer
* aftercommand:composer
* enable:composer
* disable:composer
* change_view
*/
(function(wysihtml5) {
var undef;
var defaultConfig = {
// Give the editor a name, the name will also be set as class name on the iframe and on the iframe's body
name: undef,
// Whether the editor should look like the textarea (by adopting styles)
style: true,
// Id of the toolbar element, pass falsey value if you don't want any toolbar logic
toolbar: undef,
// Whether urls, entered by the user should automatically become clickable-links
autoLink: true,
// Object which includes parser rules to apply when html gets inserted via copy & paste
// See parser_rules/*.js for examples
parserRules: { tags: { br: {}, span: {}, div: {}, p: {} }, classes: {} },
// Parser method to use when the user inserts content via copy & paste
parser: wysihtml5.dom.parse,
// Class name which should be set on the contentEditable element in the created sandbox iframe, can be styled via the 'stylesheets' option
composerClassName: "wysihtml5-editor",
// Class name to add to the body when the wysihtml5 editor is supported
bodyClassName: "wysihtml5-supported",
// By default wysihtml5 will insert a <br> for line breaks, set this to false to use <p>
useLineBreaks: true,
// Array (or single string) of stylesheet urls to be loaded in the editor's iframe
stylesheets: [],
// Placeholder text to use, defaults to the placeholder attribute on the textarea element
placeholderText: undef,
// Whether the rich text editor should be rendered on touch devices (wysihtml5 >= 0.3.0 comes with basic support for iOS 5)
supportTouchDevices: true,
// Whether senseless <span> elements (empty or without attributes) should be removed/replaced with their content
cleanUp: true
};
wysihtml5.Editor = wysihtml5.lang.Dispatcher.extend(
/** @scope wysihtml5.Editor.prototype */ {
constructor: function(textareaElement, config) {
this.textareaElement = typeof(textareaElement) === "string" ? document.getElementById(textareaElement) : textareaElement;
this.config = wysihtml5.lang.object({}).merge(defaultConfig).merge(config).get();
this.textarea = new wysihtml5.views.Textarea(this, this.textareaElement, this.config);
this.currentView = this.textarea;
this._isCompatible = wysihtml5.browser.supported();
// Sort out unsupported/unwanted browsers here
if (!this._isCompatible || (!this.config.supportTouchDevices && wysihtml5.browser.isTouchDevice())) {
var that = this;
setTimeout(function() { that.fire("beforeload").fire("load"); }, 0);
return;
}
// Add class name to body, to indicate that the editor is supported
wysihtml5.dom.addClass(document.body, this.config.bodyClassName);
this.composer = new wysihtml5.views.Composer(this, this.textareaElement, this.config);
this.currentView = this.composer;
if (typeof(this.config.parser) === "function") {
this._initParser();
}
this.on("beforeload", function() {
this.synchronizer = new wysihtml5.views.Synchronizer(this, this.textarea, this.composer);
if (this.config.toolbar) {
this.toolbar = new wysihtml5.toolbar.Toolbar(this, this.config.toolbar);
}
});
try {
console.log("Heya! This page is using wysihtml5 for rich text editing. Check out https://github.com/xing/wysihtml5");
} catch(e) {}
},
isCompatible: function() {
return this._isCompatible;
},
clear: function() {
this.currentView.clear();
return this;
},
getValue: function(parse) {
return this.currentView.getValue(parse);
},
setValue: function(html, parse) {
this.fire("unset_placeholder");
if (!html) {
return this.clear();
}
this.currentView.setValue(html, parse);
return this;
},
focus: function(setToEnd) {
this.currentView.focus(setToEnd);
return this;
},
/**
* Deactivate editor (make it readonly)
*/
disable: function() {
this.currentView.disable();
return this;
},
/**
* Activate editor
*/
enable: function() {
this.currentView.enable();
return this;
},
isEmpty: function() {
return this.currentView.isEmpty();
},
hasPlaceholderSet: function() {
return this.currentView.hasPlaceholderSet();
},
parse: function(htmlOrElement) {
var returnValue = this.config.parser(htmlOrElement, this.config.parserRules, this.composer.sandbox.getDocument(), this.config.cleanUp);
if (typeof(htmlOrElement) === "object") {
wysihtml5.quirks.redraw(htmlOrElement);
}
return returnValue;
},
/**
* Prepare html parser logic
* - Observes for paste and drop
*/
_initParser: function() {
this.on("paste:composer", function() {
var keepScrollPosition = true,
that = this;
that.composer.selection.executeAndRestore(function() {
wysihtml5.quirks.cleanPastedHTML(that.composer.element);
that.parse(that.composer.element);
}, keepScrollPosition);
});
}
});
})(wysihtml5);