-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.js
140 lines (122 loc) · 3.98 KB
/
plugin.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
/*
* Monaco Editor plugin for Reveal.js
* Version 2.0
* by Joe Skeen
* License: MIT
*/
let defaultOptions = {
monacoBaseUrl: "https://cdn.jsdelivr.net/npm/[email protected]",
selector: "pre code.monaco",
defaultLanguage: "javascript",
debug: false,
};
export class MonacoPlugin {
constructor(reveal) {
this.deck = reveal;
this.activeEditor = null;
let revealOptions = this.deck.getConfig().monaco || {};
this.options = { ...defaultOptions, ...revealOptions };
this.monacoBaseUrl = this.options.monacoBaseUrl;
}
async init() {
await new Promise((resolve) =>
this.loadScript(`${this.monacoBaseUrl}/min/vs/loader.js`, resolve)
);
require.config({ paths: { vs: `${this.monacoBaseUrl}/min/vs` } });
await new Promise((resolve) => require(["vs/editor/editor.main"], resolve));
if (window.monaco) {
this.log("[monaco] loaded");
this.monaco = window.monaco;
} else {
throw new Error("Could not load Monaco");
}
this.deck.on("slidechanged", (x) => this.onSlideChanged(x));
// see if there is an editor on the initial slide
this.onSlideChanged();
}
loadScript(url, callback) {
let head = document.querySelector("head");
let script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
// Wrapper for callback to make sure it only fires once
let finish = () => {
if (typeof callback === "function") {
callback.call();
callback = null;
}
};
script.onload = finish;
// IE
script.onreadystatechange = () => {
if (this.readyState === "loaded") {
finish();
}
};
// Normal browsers
head.appendChild(script);
}
onSlideChanged(event) {
const state = {
previousSlide: event?.previousSlide || this.deck.getPreviousSlide(),
currentSlide: event?.currentSlide || this.deck.getCurrentSlide(),
};
this.log(state);
if (state.previousSlide) {
const codeBlock = state.previousSlide.querySelector(
this.options.selector
);
if (codeBlock && this.activeEditor) {
const contents = this.activeEditor.getModel().getValue().trimStart();
this.activeEditor.dispose();
this.activeEditor = null;
const noscript = document.createElement("script");
noscript.setAttribute("type", "text/template");
noscript.innerHTML = contents;
codeBlock.appendChild(noscript);
}
}
if (state.currentSlide) {
const codeBlock = state.currentSlide.querySelector(this.options.selector);
if (codeBlock) {
const scriptTemplateChild = codeBlock.querySelector(
"script[type='text/template']"
);
const initialCode = (
scriptTemplateChild
? scriptTemplateChild.innerHTML
: codeBlock.innerHTML
).trimStart();
codeBlock.innerHTML = "";
const language =
codeBlock.getAttribute("language") || codeBlock.getAttribute("data-language") || this.options.defaultLanguage;
this.activeEditor = this.monaco.editor.create(codeBlock, {
...this.options.editorOptions,
value: initialCode,
language: language
});
this.activeEditor.getModel().onDidChangeContent(e => {
const contentChangeEvent = new CustomEvent("reveal-monaco-content-change", { detail: { textContent: this.activeEditor.getModel().getValue() } });
window.dispatchEvent(contentChangeEvent);
});
// dispatch event for initial display of the editor
window.dispatchEvent(new CustomEvent("reveal-monaco-content-change", { detail: { textContent: this.activeEditor.getModel().getValue() } }));
}
}
}
log(content) {
if (this.options.debug) {
console.info(content);
}
}
}
export const Plugin = () => {
return {
id: "monaco",
init: function (reveal) {
const plugin = new MonacoPlugin(reveal);
return plugin.init();
},
};
};
export default Plugin;