Debian Codegraph @

richtext-editor/src/RichTextEditor.qml

raw
1 /* Copyright (C) 2016 - 2018 Dan Chapman <[email protected]>
2
3 This file is part of Dekko email client for Ubuntu devices
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (at your option) version 3
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 import QtQuick 2.4
19 import Lomiri.Web 0.2
20 import com.canonical.Oxide 1.15 as Oxide
21
22 Item {
23 id: editor
24
25 /** Focused state of the editor */
26 readonly property bool focused: p.focused
27
28 /** type:string Template to load into the editor */
29 property alias template: p.template
30
31 /** Undo state of the document */
32 readonly property bool canUndo: p.undoState ? p.undoState['canUndo'] : false
33
34 /** Redo state of the document */
35 readonly property bool canRedo: p.undoState ? p.undoState['canRedo'] : false
36
37 /**
38 * type:object Font properties of the document
39 *
40 * A subset of the Font type is supported
41 *
42 * Available properties are
43 *
44 * - family:String
45 * - bold:Boolean
46 * - italic:Boolean
47 * - underline:Boolean
48 * - size:Int
49 * - textColor:Color
50 * - highlightColor:Color
51 */
52 property alias font: p.font
53
54 /**
55 * Document contents have changed
56 */
57 signal contentChanged()
58
59 /**
60 * Formatting path of the document changed
61 *
62 * This can be used to as an indication to update
63 * formatting option states
64 *
65 * @param path Formatting path to the cursor position or selected text
66 */
67 signal pathChanged(string path)
68
69 /**
70 * User has selected text in the document
71 * @param text Selected text
72 */
73 signal textSelected(string text)
74
75 /**
76 * Requested document is now ready
77 *
78 * Calls to `getDocument()` are asynchronous, on retrievel this signal is emitted with
79 * the documents contents
80 *
81 * @param document Document contents
82 */
83 signal documentReady(string document)
84
85 /**
86 * Cursor position changed in document
87 * @param rect DOMRect object of the cursor position
88 */
89 signal cursorPositionChanged(var rect)
90
91 onPathChanged: {
92 console.log("PathChange: ", path)
93 p.checkProps()
94 }
95 onFocusChanged: if (!focus) focusEditor(false)
96
97 /** Undo user action */
98 function undo() {
99 if (canUndo) {
100 wv.callFuncNoReply('undo')
101 }
102 }
103
104 /** Redo user action */
105 function redo() {
106 if (canRedo) {
107 wv.callFuncNoReply('redo')
108 }
109 }
110
111 /**
112 * Focus editor
113 *
114 * @param type:bool Should the editor get focus
115 */
116 function focusEditor(shouldFocus) {
117 if (shouldFocus) {
118 wv.callFuncNoReply('focus')
119 } else {
120 wv.callFuncNoReply('blur')
121 }
122 }
123
124 /**
125 * Request the documents contents
126 *
127 * This is an asynchronous call and the fetched document
128 * will be returned via the `documentReady` signal
129 */
130 function requestDocument() { p.getHTML(); }
131
132 /**
133 * Set the document contents
134 *
135 * @param type:string document contents as a HTML formatted string
136 */
137 function setDocument(doc) { wv.callFuncNoReply('setHTML', doc); }
138
139 /** Move cursor to start of document */
140 function moveCursorToStart() { wv.callFuncNoReply('moveCursorToStart'); }
141
142 /** Move cursor to end of document */
143 function moveCursorToEnd() { wv.callFuncNoReply('moveCursorToEnd'); }
144
145 /**
146 * Insert image into document
147 *
148 * Image is inserted at the cursors current location
149 *
150 * @param type:string Url to image source
151 */
152 function insertImage(src) {
153 // TODO
154 }
155
156 /**
157 * Insert a link into the document
158 *
159 * Link is inserted at the cursors curent position
160 *
161 * @param type:string Link url
162 * @param type:object Attributes to apply to the resulting href, e.g {'target': '_blank' }
163 */
164 function insertLink(url, attrs) {
165 wv.callFuncNoReply('makeLink', {
166 url: url,
167 attrs: attrs
168 });
169 }
170
171 /** Increase current quote level by 1 */
172 function increaseQuoteLevel() { wv.callFuncNoReply('increaseQuoteLevel'); }
173
174 /** Decrease current quote level by 1 */
175 function decreaseQuoteLevel() { wv.callFuncNoReply('decreaseQuoteLevel'); }
176
177 /** Create an unordered list */
178 function makeUnorderedList() { wv.callFuncNoReply('makeUnorderedList'); }
179
180 /** Create an ordered list */
181 function makeOrderedList() { wv.callFuncNoReply('makeOrderedList'); }
182
183 WebView {
184 id: wv
185 anchors.fill: parent
186 url: p.template
187
188 preferences {
189 javascriptEnabled: false
190 javascriptCanAccessClipboard: false
191 allowUniversalAccessFromFileUrls: false
192 allowScriptsToCloseWindows: false
193 appCacheEnabled: false
194 localStorageEnabled: false
195 hyperlinkAuditingEnabled: false
196 }
197
198 context: WebContext {
199 userScripts: [
200 Oxide.UserScript {
201 context: 'oxide://squire'
202 emulateGreasemonkey: true
203 url: Qt.resolvedUrl('./js/squire-raw.js')
204 }
205 ]
206 }
207
208 messageHandlers: [
209 Oxide.ScriptMessageHandler {
210 msgId: 'focus'
211 contexts:['oxide://squire']
212 callback: function(msg, frame) {
213 p.focused = true
214 }
215 },
216 Oxide.ScriptMessageHandler {
217 msgId: 'blur'
218 contexts:['oxide://squire']
219 callback: function(msg, frame) {
220 p.focused = false
221 }
222 },
223 Oxide.ScriptMessageHandler {
224 msgId: 'input'
225 contexts:['oxide://squire']
226 callback: function(msg, frame) {
227 editor.contentChanged()
228 }
229 },
230 Oxide.ScriptMessageHandler {
231 msgId: 'pathChange'
232 contexts:['oxide://squire']
233 callback: function(msg, frame) {
234 editor.pathChanged(msg.payload.path)
235 }
236 },
237 Oxide.ScriptMessageHandler {
238 msgId: 'select'
239 contexts:['oxide://squire']
240 callback: function(msg, frame) {
241 p.getSelectedText()
242 }
243 },
244 Oxide.ScriptMessageHandler {
245 msgId: 'cursor'
246 contexts:['oxide://squire']
247 callback: function(msg, frame) {
248 p.getCursorPosition()
249 }
250 },
251 Oxide.ScriptMessageHandler {
252 msgId: 'undoStateChange'
253 contexts:['oxide://squire']
254 callback: function(msg, frame) {
255 p.undoState = msg.payload
256 }
257 }
258 ]
259
260 onLoadProgressChanged: {
261 if (loadProgress === 100) {
262 p.initialize()
263 }
264 }
265
266 function callFunc(func, data) {
267 if (!data) {
268 data = {}
269 }
270 return wv.rootFrame.sendMessage('oxide://squire', func, data)
271 }
272
273 function callFuncNoReply(func, data) {
274 callFunc(func, data)
275 }
276 }
277
278 QtObject {
279 id: p
280 property string template: Qt.resolvedUrl("./templates/editor.html")
281 property bool focused: false
282
283 //document control
284 property var undoState: null
285 property QtObject font: QtObject {
286 // property string family: "Ubuntu"
287 property bool bold: false
288 property bool italic: false
289 property bool underline: false
290 property int size: 13
291 property color textColor: "black"
292 property color highlightColor: "white"
293 onBoldChanged: {
294 bold ? p.addBold() : p.removeBold()
295 }
296 onItalicChanged: {
297 italic ? p.addItalic() : p.removeItalic()
298 }
299 onUnderlineChanged: {
300 underline ? p.addUnderline() : p.removeUnderline()
301 }
302 }
303 property int textAlignment: undefined
304 property string selectedText: ""
305
306 onTextAlignmentChanged: {
307 var alignment = 'left';
308 switch (p.textAlignment) {
309 case Text.AlignLeft:
310 alignment = 'left' // align-left
311 break;
312 case Text.AlignHCenter:
313 alignment = 'center' // align-center
314 break;
315 case Text.AlignRight:
316 alignment = 'right' // align-right
317 break;
318 case Text.AlignJustify:
319 alignment = 'justify' // align-justify
320 break;
321 }
322 wv.callFuncNoReply('setTextAlignment', alignment)
323 }
324
325 function initialize() {
326 textAlignment = Text.AlignLeft
327 }
328
329 function addBold() {
330 wv.callFuncNoReply('bold')
331 }
332
333 function removeBold() {
334 wv.callFuncNoReply('removeBold')
335 }
336
337 function addItalic() {
338 wv.callFuncNoReply('italic')
339 }
340 function removeItalic() {
341 wv.callFuncNoReply('removeItalic')
342 }
343 function addUnderline() {
344 wv.callFuncNoReply('underline')
345 }
346 function removeUnderline() {
347 wv.callFuncNoReply('removeUnderline')
348 }
349
350 function checkProps() {
351 var props = ['B', 'U', 'I'];
352 for (var i in props) {
353 var result = wv.callFunc('hasFormat', props[i])
354 result.onreply = function(response) { updateProp(response.prop, response.value); }
355 result.onerror = function (code, response) {
356 console.warn("Failed to get format state: ", code, response)
357 }
358 }
359 }
360
361 function updateProp(prop, value) {
362 console.log("Prop: ", prop);
363 console.log("Value: ", value);
364 switch(prop) {
365 case 'B':
366 font.bold = value
367 break;
368 case 'U':
369 font.underline = value
370 break;
371 case 'I':
372 font.italic = value
373 break;
374 }
375 }
376
377 function getSelectedText() {
378 var result = wv.callFunc('getSelectedText')
379 result.onreply = function (response) {
380 selectedText = response
381 editor.textSelected(selectedText)
382 }
383 }
384
385 function getHTML() {
386 var result = wv.callFunc('getHTML')
387 result.onreply = function(response) {
388 documentReady(response)
389 }
390 result.onerror = function(code, response) {
391 console.warn("Failed to get document: ", code, response)
392 }
393 }
394
395 function getCursorPosition() {
396 var result = wv.callFunc('getCursorPosition')
397 result.onreply = function(response) {
398 cursorPositionChanged(response)
399 }
400 result.onerror = function(code, response) {
401 console.warn("Failed to get cursor position", code, response)
402 }
403 }
404
405 }
406
407 // Timer {
408 // repeat: true
409 // interval: 5000
410 // onTriggered: {
411 // if (p.textAlignment === Text.AlignLeft)
412 // p.textAlignment = Text.AlignJustify
413 // else
414 // p.textAlignment = Text.AlignLeft
415 // }
416 // Component.onCompleted: start()
417 // }
418
419 }