-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy patheditor_controller.js
More file actions
144 lines (127 loc) · 3.97 KB
/
editor_controller.js
File metadata and controls
144 lines (127 loc) · 3.97 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
import { Controller } from 'stimulus'
import CodeMirror from 'codemirror/lib/codemirror.js'
import 'codemirror/mode/htmlmixed/htmlmixed.js'
export default class extends Controller {
static targets = ['htmlContent', 'wysiwygContent', 'wysiwyg', 'html']
static values = { changed: { type: Boolean, default: false } }
initialize () {
this.tinyMCEDefaults = {
height: 500,
menubar: false,
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount'
],
toolbar: 'undo redo | formatselect | ' +
' bold italic backcolor | alignleft aligncenter ' +
' alignright alignjustify | bullist numlist outdent indent | ' +
' removeformat | image link code | help',
valid_elements: '*[*]',
forced_root_block : '',
images_upload_url: '/image_uploads',
images_file_types: 'jpeg,jpg,jpe,jfi,jif,jfif,png,gif,bmp,webp,svg',
relative_urls: false,
convert_urls: false,
init_instance_callback: (editor) => {
editor.on('input', (e) => {
this.preview(e.target.innerHTML);
this.changedValue = true;
});
editor.on('change', (e) => {
this.preview(e.target.getContent());
this.changedValue = true;
});
}
}
}
editHtml(e) {
e.preventDefault();
this.wysiwygTarget.classList.add("hidden");
this.htmlTarget.classList.remove("hidden");
this.htmlContentTarget.disabled = false;
this.initializeCodeMirror(this.wysiwygEditor.getContent());
}
initializeCodeMirror(content) {
var editor = CodeMirror.fromTextArea(this.htmlContentTarget, {
mode: "htmlmixed",
lineWrapping: true,
});
if (content) {
editor.setValue(content);
}
this.indentCodeMirror(editor);
editor.on('change', (e) => {
this.changedValue = true;
this.preview(e.getValue());
})
editor.on('drop', (e, event) => {
event.preventDefault();
this.uploadFile(event.dataTransfer.files[0], event, e)
})
return editor;
}
indentCodeMirror(editor) {
for (var i=0;i<editor.lineCount();i++) { editor.indentLine(i); }
}
preview(content) {
this.debounce(function() {
document.getElementById('hidden-preview').value = content;
document.getElementById('preview-form').submit();
}, 1000)
}
debounce(func, delay) {
if(this.timeout) { clearTimeout(this.timeout) }
this.timeout = setTimeout(func, delay);
}
wysiwyg(e) {
e.preventDefault();
this.wysiwygTarget.classList.remove("hidden");
this.htmlTarget.classList.add("hidden");
this.htmlContentTarget.disabled = true;
this.wysiwygEditor.setContent(this.htmlEditor.getValue());
this.htmlEditor.toTextArea();
}
uploadFile(file, event, editor) {
let url = '/image_uploads'
let formData = new FormData()
formData.append('file', file)
fetch(url, {
method: 'POST',
body: formData
}).then(response => response.json())
.then(data => {
let newline = `<img src="${data.location}"/>`
let doc= editor.getDoc()
editor.focus()
let x = event.pageX
let y = event.pageY
editor.setCursor(editor.coordsChar({left:x,top:y}))
let newpos = editor.getCursor()
doc.replaceRange(newline, newpos)
})
}
get wysiwygEditor() {
return tinyMCE.activeEditor;
}
get htmlEditor() {
return document.querySelector('.CodeMirror').CodeMirror;
}
leavingPage(event) {
if (this.changedValue) {
event.returnValue = "Are you sure you want to leave with unsaved changes?";
return event.returnValue;
}
}
allowFormSubmission(event) {
this.changedValue = false;
}
connect () {
let config = Object.assign({ target: this.wysiwygContentTarget }, this.tinyMCEDefaults)
tinyMCE.init(config)
this.initializeCodeMirror();
}
disconnect () {
tinyMCE.remove()
}
}