Dekko
MarkdownEditor.qml
1 import QtQuick 2.4
2 import QtQuick.Controls.Suru 2.2
3 import Lomiri.Components 1.3
4 import Dekko.Markdown 1.0
5 
6 ScrollView {
7  id: editor
8  property int frameSpacing: 0
9  property bool plainTextOnly: false
10  property alias headerHeight: col.height
11  property alias options: mdDoc.options
12  property alias text: te.text
13  property alias textDocument: te.textDocument
14 
15  property alias header: h.data
16  property alias toolbar: t.data
17 
18  signal showPreview()
19 
20  anchors.fill: parent
21 
22  Flickable {
23  id: flicker
24  anchors.fill: parent
25  contentHeight: col.height + te.paintedHeight + editor.frameSpacing + units.gu(1)
26  boundsBehavior: Flickable.StopAtBounds
27  clip: true
28 
29  Column {
30  id: col
31  anchors {
32  left: parent.left
33  top: parent.top
34  right: parent.right
35  }
36  Item {
37  id: h
38  height: childrenRect.height
39  width: parent.width
40  }
41  Item {
42  id: t
43  height: childrenRect.height
44  width: parent.width
45  }
46  Item {
47  width: parent.width
48  height: btn.height
49  visible: te.text && !editor.plainTextOnly
50  AbstractButton {
51  id: btn
52  anchors {
53  right: parent.right
54  top: parent.top
55  bottom: parent.bottom
56  }
57  width: units.gu(10)
58  height: l.height + units.gu(1)
59 
60  Label {
61  id: l
62  anchors {
63  right: parent.right
64  rightMargin: units.gu(3)
65  verticalCenter: parent.verticalCenter
66  }
67  text: qsTr("Preview")
68  color: LomiriColors.blue
69  }
70  onClicked: showPreview()
71  }
72  }
73  }
74 
75  TextEdit {
76  id: te
77  anchors {
78  left: parent.left
79  top: col.bottom
80  right: parent.right
81  leftMargin: editor.frameSpacing
82  topMargin: text && !editor.plainTextOnly ? 0 : editor.frameSpacing
83  rightMargin: editor.frameSpacing
84  }
85  height: Math.max(editor.height - col.height, contentHeight)
86  wrapMode: TextEdit.WrapAtWordBoundaryOrAnywhere
87  mouseSelectionMode: TextEdit.SelectWords
88  persistentSelection: true
89  selectByMouse: true
90  selectionColor: Qt.rgba(LomiriColors.blue.r, LomiriColors.blue.g, LomiriColors.blue.b, 0.2)
91  selectedTextColor: Suru.foregroundColor
92  color: Suru.foregroundColor
93  Keys.forwardTo: editor.plainTextOnly ? [inputOverlay] : [mdDoc, inputOverlay]
94  Mouse.forwardTo: [inputOverlay]
95 
96  InputOverlay {
97  id: inputOverlay
98  anchors.fill: parent
99  root: editor
100  textEdit: te
101  flickable: flicker
102  }
103 
104  Binding {
105  target: mdDoc
106  property: "textDocument"
107  value: te.textDocument
108  when: !editor.plainTextOnly
109  }
110 
112  id: mdDoc
113  options: FormattingOptions {
114  fontSize: FontUtils.sizeToPixels("medium")
115  enableLargeHeadingSizes: true
116  autoMatchEnabled: true
117  cycleBulletMarker: true
118  spacesForTabs: true
119  tabWidth: 4
120  textColor: Suru.secondaryForegroundColor
121  backgroundColor: Suru.backgroundColor
122  linkColor: LomiriColors.blue
123  markupColor: Suru.neutralColor
124  }
125 
126  cursorPosition: te.cursorPosition
127  onCursorPositionChanged: {
128  if (te.cursorPosition !== cursorPosition) {
129  te.cursorPosition = cursorPosition
130  }
131  }
132  hasSelection: te.selectedText
133  selectionStart: te.selectionStart
134  selectionEnd: te.selectionEnd
135  }
136  Component.onCompleted: cursorPosition = 0
137  }
138  }
139 }
FormattingOptions
Definition: FormattingOptions.h:9
Dekko
Definition: Dekko.qml:30
MarkdownDocument
Definition: MarkdownDocument.h:14
Dekko::Markdown::InputOverlay
Definition: InputOverlay.qml:6