139 lines
4.4 KiB
QML
139 lines
4.4 KiB
QML
import QtQuick 2.2
|
|
import Sailfish.Silica 1.0
|
|
import "../components/Markdown.js" as Markdown
|
|
import "../components"
|
|
|
|
Page {
|
|
id: page
|
|
|
|
property var dav
|
|
property string noteName
|
|
property string notePath: ""
|
|
property string markdown: ""
|
|
property string htmlText: ""
|
|
property string baseUrl: ""
|
|
property var imageMap: ({})
|
|
property string pendingOpen: ""
|
|
|
|
Component.onCompleted: {
|
|
// Base for relative links: the note's WebDAV folder. Relative links
|
|
// are resolved here so Qt rich text never resolves them against the
|
|
// page's .qml file (opening those fails with "text/qml not supported").
|
|
baseUrl = String(appSettings.serverUrl).replace(/\s+$/, "")
|
|
if (notePath && notePath.indexOf("/") >= 0) {
|
|
baseUrl += "/" + notePath.substring(0, notePath.lastIndexOf("/"))
|
|
}
|
|
renderPreview()
|
|
extractImages()
|
|
}
|
|
|
|
function resolveImg(u) {
|
|
if (/^[a-z]+:/i.test(u) || u.charAt(0) === "/" || u.charAt(0) === "#") return u
|
|
return String(page.baseUrl).replace(/\/+$/, "") + "/" + u
|
|
}
|
|
|
|
function renderPreview() {
|
|
try {
|
|
htmlText = Markdown.render(markdown, baseUrl,
|
|
{ imageMap: page.imageMap, imageLabel: qsTr("image") })
|
|
} catch (e) {
|
|
console.log("Quill markdown render error:", e)
|
|
htmlText = "<tt>" + String(markdown).replace(/&/g, "&").replace(/</g, "<") + "</tt>"
|
|
}
|
|
}
|
|
|
|
function extractImages() {
|
|
var re = /!\[[^\]]*\]\(([^)\s]+)\)/g
|
|
var m
|
|
while ((m = re.exec(markdown)) !== null) {
|
|
var url = resolveImg(m[1])
|
|
if (page.imageMap[url] === undefined) {
|
|
page.imageMap[url] = "" // downloading
|
|
downloader.fetch(url)
|
|
}
|
|
}
|
|
}
|
|
|
|
function openWikiLink(name) {
|
|
var dir = ""
|
|
if (notePath && notePath.indexOf("/") >= 0) {
|
|
dir = notePath.substring(0, notePath.lastIndexOf("/"))
|
|
}
|
|
page.pendingOpen = dir ? dir + "/" + name : name
|
|
dav.readFile(page.pendingOpen)
|
|
}
|
|
|
|
Connections {
|
|
target: dav
|
|
onFileReady: {
|
|
if (page.pendingOpen) {
|
|
var p = page.pendingOpen
|
|
page.pendingOpen = ""
|
|
var parts = p.split("/")
|
|
pageStack.push(Qt.resolvedUrl("EditorPage.qml"),
|
|
{ dav: page.dav, notePath: p, noteName: parts[parts.length - 1],
|
|
initialContent: content, skipLoad: true })
|
|
}
|
|
}
|
|
onFailed: {
|
|
if (page.pendingOpen) {
|
|
page.pendingOpen = ""
|
|
banner.show(qsTr("Cannot open the note: %1").arg(message), true)
|
|
}
|
|
}
|
|
}
|
|
|
|
Connections {
|
|
target: downloader
|
|
onFinished: {
|
|
if (page.imageMap[url] === undefined) return
|
|
page.imageMap[url] = ok ? path : "error"
|
|
page.renderPreview()
|
|
}
|
|
}
|
|
|
|
SilicaFlickable {
|
|
anchors.fill: parent
|
|
contentHeight: previewLabel.height + Theme.paddingLarge * 2
|
|
|
|
Column {
|
|
width: parent.width
|
|
|
|
PageHeader { title: noteName }
|
|
|
|
Label {
|
|
id: previewLabel
|
|
x: Theme.paddingLarge
|
|
width: parent.width - 2 * Theme.paddingLarge
|
|
text: page.htmlText
|
|
textFormat: Text.RichText
|
|
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
|
|
linkColor: Theme.highlightColor
|
|
onLinkActivated: {
|
|
var l = String(link)
|
|
if (l.indexOf("quill://") === 0) {
|
|
page.openWikiLink(decodeURIComponent(l.substring("quill://".length)))
|
|
} else if (l.indexOf("file:") === 0) {
|
|
// A link that still resolved to a local file (no base):
|
|
// opening it would fail with "text/qml not supported".
|
|
banner.show(qsTr("Local link not supported"), true)
|
|
} else {
|
|
Qt.openUrlExternally(l)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
VerticalScrollDecorator {}
|
|
|
|
PullDownMenu {
|
|
MenuItem {
|
|
text: qsTr("Edit")
|
|
onClicked: pageStack.pop()
|
|
}
|
|
}
|
|
}
|
|
|
|
Banner { id: banner }
|
|
}
|