-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick link.js
192 lines (190 loc) · 7.42 KB
/
quick link.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
function error(text) {
return ui.notifications.error(text);
}
function replaceAll(s, original, replacement) {
let res = s;
let iter = res.replace(original, replacement);
while (res !== iter) {
res = iter;
iter = res.replace(original, replacement);
}
return res;
}
function findFirstDiffPos(a, b, startA = 0, startB = 0)
{
let aSlice = a.slice(startA)
let bSlice = b.slice(startB)
let shorterLength = Math.min(aSlice.length, bSlice.length);
for (let i = 0; i > shorterLength; i++)
{
if (aSlice[i] !== bSlice[i]) return i;
}
if (aSlice.length !== bSlice.length) return shorterLength;
return -1;
}
function _replaceTextContent(text, rgx, func) {
let replaced = false;
for ( let t of text ) {
const matches = t.textContent.matchAll(rgx);
for ( let match of Array.from(matches).reverse() ) {
const replacement = func(...match);
if ( replacement ) {
let matched_text = match[0];
let div = document.createElement("div");
div.innerHTML = '__start_replacement__' + '__replacement_start_flag__' + replacement.outerHTML + '__replacement_mid_flag__' + matched_text + '__replacement_end_flag__' + '__end_replacement__';
TextEditor._replaceTextNode(t, match, div);
replaced = true;
}
}
}
return replaced;
}
function enrichHTML(content, {secrets=false, entities=true, links=true, rolls=true, rollData=null}={}) {
// Create the HTML element
const html = document.createElement("div");
html.innerHTML = String(content);
// Remove secret blocks
if ( !secrets ) {
let elements = html.querySelectorAll("section.secret");
elements.forEach(e => e.parentNode.removeChild(e));
}
// Plan text content replacements
let updateTextArray = true;
let text = [];
// Replace entity links
if ( entities ) {
if ( updateTextArray ) text = TextEditor._getTextNodes(html);
const entityTypes = CONST.ENTITY_LINK_TYPES.concat("Compendium");
const rgx = new RegExp(`@(${entityTypes.join("|")})\\[([^\\]]+)\\](?:{([^}]+)})?`, 'g');
updateTextArray = _replaceTextContent(text, rgx, TextEditor._createEntityLink);
}
// Replace hyperlinks
if ( links ) {
if ( updateTextArray ) text = TextEditor._getTextNodes(html);
const rgx = /(https?:\/\/)(www\.)?([^\s<]+)/gi;
updateTextArray = _replaceTextContent(text, rgx, TextEditor._createHyperlink);
}
// Replace inline rolls
if ( rolls ) {
if (updateTextArray) text = TextEditor._getTextNodes(html);
const rgx = /\[\[(\/[a-zA-Z]+\s)?(.*?)([\]]{2,3})/gi;
updateTextArray = _replaceTextContent(text, rgx, (...args) => TextEditor._createInlineRoll(...args, rollData));
}
// Return the enriched HTML
return html.innerHTML;
}
function processHTML(html) {
let precessedHTML = enrichHTML(html);
precessedHTML = replaceAll(precessedHTML, '<div>__start_replacement__', '')
precessedHTML = replaceAll(precessedHTML, '__end_replacement__</div>', '')
return precessedHTML;
}
function getReplacements(rawHTML) {
const replacementRegex = /__replacement_start_flag__(?<pretty>.+?)__replacement_mid_flag__(?<raw>.+?)__replacement_end_flag__/g
let processedHTML = processHTML(rawHTML);
return [...processedHTML.matchAll(replacementRegex)].map(m => m.groups);
}
function deprettify(prettyHTML, replacements) {
let res = prettyHTML;
for (let replacement of replacements) {
res = res.replace(replacement.pretty, replacement.raw)
}
return res;
}
function getJournalEntry(node, maxDepth=20) {
if (maxDepth < 0) return null;
if (node.getAttribute && node.getAttribute('id')) {
let journalRawID = node.getAttribute('id');
let journalIDRegex = /journal-(?<id>\w*)/;
let JournalID = journalRawID.match(journalIDRegex).groups.id;
return game.journal.get(JournalID);
}
if (!node.parentNode) return null;
return getJournalEntry(node.parentNode, maxDepth-1)
}
function getJournalFrame(node, maxDepth=20) {
if (maxDepth < 0) return null;
if (node.className === "editor-content") return node;
if (!node.parentNode) return null;
return getJournalFrame(node.parentNode, maxDepth-1)
}
async function replaceSelection(selection, text) {
let journalEntry = getJournalEntry(selection.baseNode);
let journalFrame = getJournalFrame(selection.baseNode);
if (!journalEntry) return error("failed to get journal entry");
if (!journalFrame) return error("failed to get journal frame");
let rawHTML = journalEntry.data.content;
let replacements = getReplacements(rawHTML);
let selectionRange = selection.getRangeAt(0);
selectionRange.deleteContents();
selectionRange.insertNode(document.createTextNode(text));
let deprettifiedHTML = deprettify(journalFrame.innerHTML, replacements);
await journalEntry.update({content: deprettifiedHTML});
}
async function getPacksContents() {
let packRegex = /^(?<moduleName>[\w-]+)\.(?<packName>[\w-]+)$/;
let packs = {}
for (let pack of game.packs.entries){
let {moduleName, packName} = pack.collection.match(packRegex).groups;
if (!packs[moduleName]) packs[moduleName] = {};
packs[moduleName][packName] = pack;
}
let packsContents = [];
for (let moduleName of Object.keys(packs)) {
for (let packName of Object.keys(packs[moduleName])) {
let pack = packs[moduleName][packName];
packsContents.push(...pack.index.filter(entry => entry.name !== '#[CF_tempEntity]').map(entry => ({name: entry.name, module: moduleName, pack: packName, type: pack.entity, id: entry._id})));
}
}
return packsContents;
}
function levenshteinDistance(a, b) {
let tmp;
if (a.length === 0) { return b.length; }
if (b.length === 0) { return a.length; }
if (a.length < b.length) { tmp = a; a = b; b = tmp; }
let i, j, res, alen = a.length, blen = b.length, row = Array(alen);
for (i = 0; i <= alen; i++) { row[i] = i; }
for (i = 1; i <= blen; i++) {
res = i;
for (j = 1; j <= alen; j++) {
tmp = row[j - 1];
row[j - 1] = res;
res = b[i - 1] === a[j - 1] ? tmp : Math.min(tmp + 1, Math.min(res + 1, row[j] + 1));
}
}
return res;
}
function cleanName(text) {
return text.toLowerCase().match(/^(\d+ )?(?<core>.*?)( \(.+\))?$/).groups.core;
}
function sortPacks(packs, target) {
return packs.map(pack => ({pack, distance: levenshteinDistance(cleanName(pack.name), (target))}))
.sort((pd1, pd2) => pd1.distance - pd2.distance)
.map(pd => pd.pack);
}
function getLink(entry, text) {
return `@Compendium[${entry.module}.${entry.pack}.${entry.name}]{${text}}`
}
async function renderEntry(entry) {
let pack = game.packs.get(`${entry.module}.${entry.pack}`);
let entity = await pack.getEntity(entry.id);
return entity.sheet.render(true)
}
async function main(){
let selection = window.getSelection();
let startNode = selection.baseNode;
if (!(startNode.nodeType === 3)) return error("startNode isn't text");
let endNode = selection.focusNode;
if (!(startNode.nodeType === 3)) return error("endNode isn't text");
if (!(startNode.parentNode === endNode.parentNode))
return error("startNode and endNode have different parents");
let selectionText = selection.toString();
let packsContents = await getPacksContents();
let sortedContents = sortPacks(packsContents, selectionText);
let closestEntry = sortedContents[0];
let closestLink = getLink(closestEntry, selectionText);
replaceSelection(selection, closestLink);
renderEntry(closestEntry);
}
main();