Files
harbour-quill/qml/pages/PreviewPage.qml
T
kaneda 8259297a27 0.3.0: ricerca nel testo, caselle di spunta toccabili, tag
- Ricerca nel testo: la cartella viene percorsa ricorsivamente e le note .md
  scaricate una alla volta (il WebDAV non cerca nel contenuto), con avanzamento,
  possibilità di fermare e il passaggio che contiene la parola. La ricerca per
  titolo nella lista resta istantanea.
- Caselle di spunta: "- [ ]" e "- [x]" diventano caselle toccabili
  nell'anteprima; il tocco riscrive quella sola riga e salva subito, e l'editor
  segue (senza, uscendo riscriverebbe la versione vecchia sul server).
- Tag: front-matter (tags: [a, b], lista a trattini o separata da virgole) e
  #tag nel testo; pastiglie in cima all'anteprima, #tag toccabili, nuova
  schermata Tag con il conteggio per tag. Il blocco front-matter non viene più
  stampato come testo.
- tests/qml_contract_check.py: confronta i nomi JS e le proprietà usati dalla
  QML con i sorgenti, le pagine raggiungibili e i parametri dei gestori dei
  segnali — la classe di errori che la compilazione non vede.
- Catalogo italiano aggiornato (25 stringhe nuove); repo ripulito dai residui di
  build e .gitignore esteso.
2026-11-08 20:55:22 +01:00

218 lines
7.6 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: ""
// The editor below: it must follow every change made here (a checked box),
// otherwise leaving the editor would save the old text back.
property var owner: null
// Front-matter tags: the block itself is not rendered, these chips are it.
property var fmTags: []
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("/"))
}
refreshTags()
renderPreview()
extractImages()
}
function refreshTags() {
try {
page.fmTags = Markdown.tagDetails(page.markdown).frontMatter
} catch (e) {
page.fmTags = []
}
}
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"),
checkOff: Theme.secondaryColor,
checkOn: Theme.highlightColor })
} catch (e) {
console.log("Quill markdown render error:", e)
htmlText = "<tt>" + String(markdown).replace(/&/g, "&amp;").replace(/</g, "&lt;") + "</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)
}
// A tapped checkbox rewrites exactly one line and saves it at once: the
// note on the server is the only copy, so waiting for the editor to be left
// would lose the tick if the app were closed first.
function toggleLine(lineIndex) {
var next = Markdown.toggleTask(page.markdown, lineIndex)
if (next === null) {
banner.show(qsTr("This line is not a task"), true)
return
}
page.markdown = next
page.refreshTags()
page.renderPreview()
if (page.owner && page.owner.externalUpdate) page.owner.externalUpdate(next, false)
if (page.dav.busy) {
banner.show(qsTr("Save in progress, the change is kept"), false)
return
}
page.dav.writeFile(page.notePath, next)
}
function openTag(tag) {
pageStack.push(Qt.resolvedUrl("SearchPage.qml"),
{ dav: page.dav, tagFilter: tag, rootPath: "" })
}
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)
} else {
banner.show(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: column.height + Theme.paddingLarge * 2
Column {
id: column
width: parent.width
PageHeader { title: noteName }
Flow {
x: Theme.paddingLarge
width: column.width - 2 * Theme.paddingLarge
spacing: Theme.paddingSmall
visible: page.fmTags.length > 0
Repeater {
model: page.fmTags
delegate: Rectangle {
width: tagLabel.implicitWidth + Theme.paddingMedium * 2
height: tagLabel.implicitHeight + Theme.paddingSmall
radius: height / 2
color: Theme.rgba(Theme.highlightBackgroundColor, 0.2)
Label {
id: tagLabel
anchors.centerIn: parent
text: "#" + modelData
color: Theme.highlightColor
font.pixelSize: Theme.fontSizeExtraSmall
}
MouseArea {
anchors.fill: parent
onClicked: page.openTag(modelData)
}
}
}
}
Label {
id: previewLabel
x: Theme.paddingLarge
width: parent.width - 2 * Theme.paddingLarge
topPadding: Theme.paddingMedium
text: page.htmlText
textFormat: Text.RichText
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
linkColor: Theme.highlightColor
onLinkActivated: {
var l = String(link)
if (l.indexOf("quill-check://") === 0) {
page.toggleLine(parseInt(l.substring("quill-check://".length), 10))
} else if (l.indexOf("quill-tag://") === 0) {
page.openTag(decodeURIComponent(l.substring("quill-tag://".length)))
} else 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 }
}