/
setup.ts
135 lines (109 loc) · 3.84 KB
/
setup.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { usePageFrontmatter, usePageData } from "@vuepress/client";
import { isLinkHttp, isPlainObject, removeEndingSlash } from "@vuepress/shared";
import { useEventListener } from "@vueuse/core";
import { computed, onMounted, watchEffect } from "vue";
import { useRoute } from "vue-router";
import { useLocaleConfig } from "vuepress-shared/client";
import type { RequiredLocaleConfig } from "vuepress-shared/client";
import type {
CopyrightLocaleData,
CopyrightPluginFrontmatter,
CopyrightPluginPageData,
} from "../../shared/index.js";
declare const COPYRIGHT_CANONICAL: string;
declare const COPYRIGHT_DISABLE_COPY: boolean;
declare const COPYRIGHT_DISABLE_SELECTION: boolean;
declare const COPYRIGHT_GLOBAL: boolean;
declare const COPYRIGHT_LOCALES: RequiredLocaleConfig<CopyrightLocaleData>;
declare const COPYRIGHT_TRIGGER_WORDS: number;
const canonical = COPYRIGHT_CANONICAL;
export const setupCopyright = (): void => {
const frontmatter = usePageFrontmatter<CopyrightPluginFrontmatter>();
const locale = useLocaleConfig(COPYRIGHT_LOCALES);
const page = usePageData<CopyrightPluginPageData>();
const route = useRoute();
const enabled = computed(
() =>
Boolean(frontmatter.value.copy) ||
(frontmatter.value.copy !== false && COPYRIGHT_GLOBAL)
);
const disableCopy = computed(() => {
const frontmatterOptions = frontmatter.value.copy;
if (!enabled.value) return false;
if (
isPlainObject(frontmatterOptions) &&
"disableCopy" in frontmatterOptions
)
return frontmatterOptions.disableCopy;
return COPYRIGHT_DISABLE_COPY;
});
const disableSelection = computed(() => {
const frontmatterOptions = frontmatter.value.copy;
if (!enabled.value) return false;
if (
isPlainObject(frontmatterOptions) &&
"disableSelection" in frontmatterOptions
)
return frontmatterOptions.disableSelection;
return COPYRIGHT_DISABLE_SELECTION;
});
const getCopyright = (): string => {
const { author: authorInfo = "", license: licenseInfo = "" } =
page.value.copyright;
const { author, license, link } = locale.value;
return [
authorInfo ? author.replace(":author", authorInfo) : "",
licenseInfo ? license.replace(":license", licenseInfo) : "",
link.replace(
":link",
canonical
? `${removeEndingSlash(
isLinkHttp(canonical) ? canonical : `https://${canonical}`
)}${route.path}`
: window.location.href
),
]
.filter((item) => item)
.join("\n");
};
const onCopy = (event: ClipboardEvent): void => {
const selection = getSelection();
if (selection) {
const textRange = selection.getRangeAt(0);
if (enabled.value) {
if (disableCopy.value) {
event.preventDefault();
return;
}
if (textRange.toString().length >= COPYRIGHT_TRIGGER_WORDS) {
event.preventDefault();
const copyright = getCopyright();
const node = document.createElement("div");
node.appendChild(selection.getRangeAt(0).cloneContents());
if (event.clipboardData) {
event.clipboardData.setData(
"text/html",
`${node.innerHTML}<hr><div class="copyright">${copyright.replace(
/\\n/g,
"<br>"
)}</div>`
);
event.clipboardData.setData(
"text/plain",
`${
selection.getRangeAt(0).cloneContents().textContent || ""
}\n------\n${copyright}`
);
}
}
}
}
};
onMounted(() => {
const appElement = document.querySelector<HTMLDivElement>("#app")!;
useEventListener(appElement, "copy", onCopy);
watchEffect(() => {
appElement.style.userSelect = disableSelection.value ? "none" : "auto";
});
});
};