-
Notifications
You must be signed in to change notification settings - Fork 22
/
froala-editor.js
88 lines (76 loc) · 2.16 KB
/
froala-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
import {inject, customElement, bindable} from 'aurelia-framework';
import {ObserverLocator} from "aurelia-binding";
import {Config} from './froala-editor-config';
// Import Froala Editor
import FroalaEditor from 'froala-editor'
@customElement('froala-editor')
@inject(Element, Config, ObserverLocator)
export class FroalaEditor1 {
@bindable value;
@bindable config = {};
@bindable eventHandlers = {};
@bindable editor;
parent;
element;
constructor (element, config, observerLocator) {
this.element = element;
this.config = config.options();
this.observerLocator = observerLocator;
}
// Get parent context to use in eventhandlers
bind(bindingContext, overrideContext) {
this.parent = bindingContext;
}
// Setup
attached() {
// Get element.
const editorSelector = this.config.iframe ? 'textarea' : 'div';
let editor = this
// Check if editor isn't already initialized.
if (this.editor != null) { return; }
// Observe value.
this.subscriptions = [
this.observerLocator
.getObserver(this, 'value')
.subscribe((newValue, oldValue) => {
if (this.editor && this.editor.html.get() != newValue) {
this.editor.html.set(newValue);
}
})
];
// Will be registered when editor is initialized.
this.config.events = {
contentChanged: function contentChanged(e) {
return editor.value = this.html.get();
},
blur: function blur(e) {
return editor.value = this.html.get();
}
};
// Initialize editor.
this.editor = new FroalaEditor(this.element, Object.assign({}, this.config), () => {
// Set initial HTML value.
this.editor.html.set(this.value);
// Set Events
if (this.eventHandlers && this.eventHandlers.length != 0) {
for(let eventHandlerName in this.eventHandlers) {
let handler = this.eventHandlers[eventHandlerName];
if (eventHandlerName === 'initialized') {
handler.apply(this.parent);
} else {
this.editor.events.on(`${eventHandlerName}`, (...args) => {
return handler.apply(this.parent, args);
});
}
}
}
});
}
// Destroy
detached () {
if (this.editor != null) {
this.editor.destroy();
this.editor = null;
}
}
}