1
0
This commit is contained in:
Zef Hemel 2023-01-13 16:33:36 +01:00
parent a56e14bff1
commit a170d9c2d7
3 changed files with 42 additions and 33 deletions

View File

@ -5,7 +5,7 @@ syntax:
Hashtag:
firstCharacters:
- "#"
regex: "#[^#\\d\\s]+\\w+"
regex: "#[^#\\d\\s\\[\\]]+\\w+"
className: sb-hashtag
NakedURL:
firstCharacters:

View File

@ -36,45 +36,31 @@ export function linkPlugin(editor: Editor) {
const cleanAnchor = anchorPart.substring(1); // cut off the initial [
const cleanLink = linkPart.substring(0, linkPart.length - 1); // cut off the final )
// Hide the whole thing
// Hide the start [
widgets.push(
invisibleDecoration.range(
from,
from + 1,
),
);
// Wrap the link in a href
widgets.push(
Decoration.mark({
tagName: "a",
class: "sb-link",
attributes: {
href: cleanLink,
title: `Click to visit ${cleanLink}`,
},
}).range(from + 1, from + cleanAnchor.length + 1),
);
// Hide the tail end of the link
widgets.push(
invisibleDecoration.range(
from + cleanAnchor.length + 1,
to,
),
);
widgets.push(
Decoration.widget({
widget: new LinkWidget(
{
text: cleanAnchor,
title: `Click to visit ${cleanLink}`,
cssClass: "sb-link",
href: cleanLink,
callback: (e) => {
if (e.altKey) {
// Move cursor into the link, approximate location
return editor.editorView!.dispatch({
selection: { anchor: from + 1 },
});
}
// Dispatch click event to navigate there without moving the cursor
const clickEvent: ClickEvent = {
page: editor.currentPage!,
ctrlKey: e.ctrlKey,
metaKey: e.metaKey,
altKey: e.altKey,
pos: from,
};
editor.dispatchAppEvent("page:click", clickEvent).catch(
console.error,
);
},
},
),
}).range(from),
);
},
});

View File

@ -623,6 +623,29 @@ export class Editor {
}
touchCount = 0;
},
mousedown: (event: MouseEvent, view: EditorView) => {
// Make sure <a> tags are clicked without moving the cursor there
if (!event.altKey && event.target instanceof Element) {
const parentA = event.target.closest("a");
if (parentA) {
event.stopPropagation();
event.preventDefault();
const clickEvent: ClickEvent = {
page: pageName,
ctrlKey: event.ctrlKey,
metaKey: event.metaKey,
altKey: event.altKey,
pos: view.posAtCoords({
x: event.x,
y: event.y,
})!,
};
this.dispatchAppEvent("page:click", clickEvent).catch(
console.error,
);
}
}
},
click: (event: MouseEvent, view: EditorView) => {
safeRun(async () => {
const clickEvent: ClickEvent = {