-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkirbytext.js
More file actions
44 lines (40 loc) · 1.05 KB
/
kirbytext.js
File metadata and controls
44 lines (40 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
let router;
const origin = import.meta.env.DEV
? `${import.meta.env.KIRBY_DEV_PROTOCOL}://${
import.meta.env.KIRBY_DEV_HOSTNAME
}:${import.meta.env.KIRBY_DEV_PORT}`
: window.location.origin;
/**
* Handle absolute links inside dynamically added HTML with Vue Router
*
* @param {Event} event The event taking place in the DOM
*/
function navigate(event) {
const link = event.target.closest("a");
if (
link &&
link.href.startsWith(origin) &&
link.target !== "_blank" &&
!event.defaultPrevented &&
event.button === 0 &&
!event.metaKey &&
!event.altKey &&
!event.ctrlKey &&
!event.shiftKey
) {
event.preventDefault();
router.push({ path: link.href.slice(origin.length) });
}
}
/** @param {import("vue").App} app The Vue app instance */
export const install = (app) => {
app.directive("kirbytext", {
mounted(el, binding) {
router = binding.instance.$router;
el.addEventListener("click", navigate);
},
beforeUnmount(el) {
el.removeEventListener("click", navigate);
},
});
};