19import QtQuick.Controls.Suru 2.2
20import Lomiri.Components 1.3
21import Lomiri.Components.Popups 1.3
22import Dekko.Mail.Stores.Composer 1.0
23import Dekko.Lomiri.Constants 1.0
24import Dekko.Editor 1.0
27 id: htmlEditorContainer
29 property alias editor: htmlEditor
30 property string currentFontSize: "11pt"
31 readonly
property color defaultTextColor: Suru.foregroundColor
32 property color currentTextColor: defaultTextColor
33 property color currentHighlightColor:
"transparent"
35 property string pendingFontSize:
""
36 property string pendingHighlightColor:
""
37 property string pendingTextColor:
""
40 property bool isEditorLoaded:
false
41 property string pendingDocument:
""
42 property string currentDocument:
""
43 property bool isInternallyUpdating:
false
45 function setHtmlDocument(html) {
46 currentDocument = html ||
""
47 if (htmlEditorContainer.visible && isEditorLoaded && htmlEditor) {
48 htmlEditor.setDocument(html)
50 pendingDocument = html ||
""
58 htmlEditor.focusEditor(
true)
59 if (pendingFontSize !==
"") {
60 htmlEditor.setFontSize(pendingFontSize)
63 if (pendingHighlightColor !==
"") {
64 htmlEditor.setHighlightColor(pendingHighlightColor)
65 pendingHighlightColor =
""
67 if (pendingTextColor !==
"") {
68 htmlEditor.setTextColor(pendingTextColor)
75 function rgbToHex(rgbStr) {
76 if (!rgbStr)
return defaultTextColor
79 const match = rgbStr.match(/rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/)
81 const r = parseInt(match[1])
82 const g = parseInt(match[2])
83 const b = parseInt(match[3])
85 const hex =
"#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)
91 function parsePathForFormatting(path) {
92 const parts = path.split(
'>')
94 let color = defaultTextColor
95 let highlight =
"transparent"
98 for (let i = 0; i < parts.length; i++) {
102 const fontSizeMatch = part.match(/fontSize=([^\]]+)/)
104 size = fontSizeMatch[1]
108 const colorMatch = part.match(/\[color=([^\]]+)\]/)
110 color = rgbToHex(colorMatch[1])
114 const highlightMatch = part.match(/backgroundColor=([^\]]+)/)
115 if (highlightMatch) {
116 highlight = rgbToHex(highlightMatch[1])
121 currentFontSize = size
122 currentTextColor = color
123 currentHighlightColor = highlight
134 currentFontSize: htmlEditorContainer.currentFontSize
135 currentTextColor: htmlEditorContainer.currentTextColor
136 currentHighlightColor: htmlEditorContainer.currentHighlightColor
139 PopupUtils.open(fontSizeDialogComponent)
142 onTextColorClicked: {
143 PopupUtils.open(colorPickerDialogComponent, undefined, {isHighlight:
false})
146 onHighlightColorClicked: {
147 PopupUtils.open(colorPickerDialogComponent, undefined, {isHighlight:
true})
151 PopupUtils.open(linkDialogComponent)
158 height: parent.height - htmlToolbar.height
160 function requestContentUpdate() {
165 if (htmlEditorContainer.visible && htmlEditorContainer.isEditorLoaded && htmlEditorContainer.pendingDocument !==
"") {
166 htmlEditor.setDocument(htmlEditorContainer.pendingDocument)
167 htmlEditorContainer.pendingDocument =
""
172 htmlEditorContainer.isEditorLoaded =
true
173 if (htmlEditorContainer.visible && htmlEditorContainer.pendingDocument !==
"") {
174 htmlEditor.setDocument(htmlEditorContainer.pendingDocument)
175 htmlEditorContainer.pendingDocument =
""
180 htmlEditorContainer.currentDocument = document ||
""
181 htmlEditorContainer.isInternallyUpdating =
true
182 if (ComposerStore.htmlContent !== htmlEditorContainer.currentDocument) {
183 ComposerStore.htmlContent = htmlEditorContainer.currentDocument
185 htmlEditorContainer.isInternallyUpdating =
false
193 htmlEditorContainer.parsePathForFormatting(path)
198 running: htmlEditorContainer.visible && htmlEditorContainer.isEditorLoaded
200 onTriggered: htmlEditor.requestDocument()
205 id: linkDialogComponent
207 onLinkInserted: function(url, text) {
209 if (text && text.length > 0) {
210 htmlEditor.insertLinkWithText(url, text, { target:
'_blank' })
212 htmlEditor.insertLink(url, { target:
'_blank' })
219 id: fontSizeDialogComponent
222 htmlEditorContainer.currentFontSize = size
223 htmlEditorContainer.pendingFontSize = size
224 applyFormatTimer.restart()
230 id: colorPickerDialogComponent
234 htmlEditorContainer.currentHighlightColor = color
235 htmlEditorContainer.pendingHighlightColor = color
237 htmlEditorContainer.currentTextColor = color
238 htmlEditorContainer.pendingTextColor = color
240 applyFormatTimer.restart()