-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbbscript.js
More file actions
159 lines (149 loc) · 4.71 KB
/
bbscript.js
File metadata and controls
159 lines (149 loc) · 4.71 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
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
import { debounce } from "@ember/runloop";
import loadscript from "discourse/lib/load-script";
import { withPluginApi } from "discourse/lib/plugin-api";
/* global bbscriptParser */
/**
* @type {Array<{callback: function, on: string}>}
*/
let attachedPreviewBBScripts = [];
/** @type {WeakMap<Element, CallableFunction[]>} */
const initBBScripts = new WeakMap();
const PARENT_PREVIEW_WRAPPER_CLASS = "d-editor-preview";
const documentObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const post = entry.target;
const callback = initBBScripts.get(post);
callback?.forEach((c) => c());
initBBScripts.delete(post);
observer.unobserve(post);
}
});
},
{
threshold: 0,
rootMargin: "10px 0px 0px 0px",
}
);
/**
* Check if the post is a preview. If it is a preview, debounce the function
* @param {HTMLElement} post
*/
function checkIsPreview(post) {
let isPreview = post.classList.contains(PARENT_PREVIEW_WRAPPER_CLASS);
if (isPreview) {
// prevent multiple calls to addBBScriptLogic
// preview mode is constantly updating based on user input
debounce(this, addBBScriptLogic, post, true, 1000);
} else {
addBBScriptLogic(post, false);
}
}
/**
* Adds the bbscript functions to the post
* @param {HTMLElement} post the post itself
*/
function addBBScriptLogic(post, isPreview = false) {
if (isPreview) {
attachedPreviewBBScripts.forEach(({ callback, on }) => {
post.removeEventListener(on, callback, true);
});
attachedPreviewBBScripts = [];
delete bbscriptParser.bbscriptData.preview;
}
post.querySelectorAll("template[data-bbcode-plus='script']").forEach((el) => {
const callerId = el.getAttribute("data-bbscript-id") || "";
const callerClass = el.getAttribute("data-bbscript-class") || "";
/** @type {string} */
const content = el.content.textContent || "";
let version = el.getAttribute("data-bbscript-ver") || "";
const on = el.getAttribute("data-bbscript-on") || "init"; // valid on events: init, click, mouseover, mouseout, etc.
let astTree;
if (version === "") {
// unknown version. check for unique () style of bbscript2
version = content.split("\n").some((line) => line.trim().startsWith("(")) ? "2" : "1";
}
if (version === "2") {
const parsed = bbscriptParser.bbscript2Parser.parse(content);
astTree = parsed.ast;
if (parsed.formattedErrors.length) {
// eslint-disable-next-line no-console
console.warn(parsed.formattedErrors);
}
} else {
astTree = bbscriptParser.bbscriptProcessorV1.parse(content);
}
if (on === "init") {
let target;
if (callerClass) {
target = document.querySelectorAll("." + callerClass + "__" + callerId) || undefined;
}
// only fire when the post is visible
if (!initBBScripts.has(post)) {
initBBScripts.set(post, []);
}
initBBScripts.get(post).push(() => {
triggerBBScript(callerId, callerClass, astTree, version, target);
});
documentObserver.observe(post);
} else {
const callback = (ev) => {
const target = ev.target?.closest("." + callerClass + "__" + callerId);
if (target) {
triggerBBScript(callerId, callerClass, astTree, version, target);
}
};
// event delegation
post.addEventListener(on, callback, true);
if (isPreview) {
attachedPreviewBBScripts.push({ callback, on });
}
}
});
}
/**
* @param {string} callerId
* @param {string} callerClass
* @param {ASTNode[] | astNode[]} astTree
* @param {string} version
* @param {Element | NodeListOf<Element>} [target]
* @returns {void}
*/
const triggerBBScript = (callerId, callerClass, astTree, version, target) => {
if (version === "1") {
bbscriptParser.bbscriptProcessorV1.execAll(astTree, callerId, callerClass, { target });
} else if (version === "2") {
try {
bbscriptParser.bbscriptProcessorV2.execAll(astTree, callerId, callerClass, { target });
} catch (e) {
if (e?.message !== "BBScript Stop Command") {
// eslint-disable-next-line no-console
console.warn(e);
}
}
}
};
function initializeBBScript(api) {
const siteSettings = api.container.lookup("service:site-settings");
if (!siteSettings.enable_bbscript) {
return;
}
api.decorateCookedElement(
(post) => {
loadscript("/plugins/bbcode/javascripts/bbscript-parser.min.js").then(() => {
checkIsPreview(post);
});
},
{
id: "add bbscript",
afterAdopt: true,
}
);
}
export default {
name: "bbscript",
initialize() {
withPluginApi("0.11.1", initializeBBScript);
},
};