Fixes to missing link marking

pull/138/head
Zef Hemel 2022-11-30 15:28:43 +01:00
parent 477f021644
commit a41695bc67
4 changed files with 66 additions and 54 deletions

View File

@ -56,10 +56,12 @@ export function linkPlugin(editor: Editor) {
widgets.push( widgets.push(
Decoration.widget({ Decoration.widget({
widget: new LinkWidget( widget: new LinkWidget(
cleanAnchor, {
`Click to visit ${cleanLink}`, text: cleanAnchor,
"sb-link", title: `Click to visit ${cleanLink}`,
(e) => { cssClass: "sb-link",
href: cleanLink,
callback: (e) => {
if (e.altKey) { if (e.altKey) {
// Move cursor into the link, approximate location // Move cursor into the link, approximate location
return view.dispatch({ return view.dispatch({
@ -78,6 +80,7 @@ export function linkPlugin(editor: Editor) {
console.error, console.error,
); );
}, },
},
), ),
}).range(from), }).range(from),
); );

View File

@ -11,26 +11,30 @@ import {
WidgetType, WidgetType,
} from "../deps.ts"; } from "../deps.ts";
type LinkOptions = {
text: string;
href?: string;
title: string;
cssClass: string;
callback: (e: MouseEvent) => void;
};
export class LinkWidget extends WidgetType { export class LinkWidget extends WidgetType {
constructor( constructor(
readonly text: string, readonly options: LinkOptions,
readonly title: string,
readonly cssClass: string,
readonly callback: (e: MouseEvent) => void,
) { ) {
super(); super();
} }
toDOM(): HTMLElement { toDOM(): HTMLElement {
const anchor = document.createElement("a"); const anchor = document.createElement("a");
anchor.className = this.cssClass; anchor.className = this.options.cssClass;
anchor.textContent = this.text; anchor.textContent = this.options.text;
anchor.addEventListener("click", (e) => { anchor.addEventListener("click", (e) => {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
this.callback(e); this.options.callback(e);
}); });
anchor.setAttribute("title", this.title); anchor.setAttribute("title", this.options.title);
anchor.href = "#"; anchor.href = this.options.href || "#";
return anchor; return anchor;
} }
} }

View File

@ -89,12 +89,16 @@ export function cleanWikiLinkPlugin(editor: Editor) {
widgets.push( widgets.push(
Decoration.widget({ Decoration.widget({
widget: new LinkWidget( widget: new LinkWidget(
linkText, {
pageExists ? `Navigate to ${page}` : `Create ${page}`, text: linkText,
pageExists title: pageExists
? `Navigate to ${page}`
: `Create ${page}`,
href: `/${page.replaceAll(" ", "_")}`,
cssClass: pageExists
? "sb-wiki-link-page" ? "sb-wiki-link-page"
: "sb-wiki-link-page-missing", : "sb-wiki-link-page-missing",
(e) => { callback: (e) => {
if (e.altKey) { if (e.altKey) {
// Move cursor into the link // Move cursor into the link
return view.dispatch({ return view.dispatch({
@ -113,6 +117,7 @@ export function cleanWikiLinkPlugin(editor: Editor) {
console.error, console.error,
); );
}, },
},
), ),
}).range(from), }).range(from),
); );

View File

@ -348,7 +348,7 @@
text-decoration: none; text-decoration: none;
cursor: pointer; cursor: pointer;
} }
.sb-wiki-link-page-missing, .sb-wiki-link-page-missing > .sb-wiki-link-page { a.sb-wiki-link-page-missing, .sb-wiki-link-page-missing > .sb-wiki-link-page {
color: #9e4705; color: #9e4705;
background-color: rgba(77, 141, 255, 0.07); background-color: rgba(77, 141, 255, 0.07);
border-radius: 5px; border-radius: 5px;