forked from scratchblocks/scratchblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (142 loc) · 3.83 KB
/
index.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
/*
* scratchblocks
* http://scratchblocks.github.io/
*
* Copyright 2013-2016, Tim Radvan
* @license MIT
* http://opensource.org/licenses/MIT
*/
import {
parse,
allLanguages,
loadLanguages,
Label,
Icon,
Input,
Block,
Comment,
Script,
Document,
} from "./syntax/index.js"
import * as scratch2 from "./scratch2/index.js"
import * as scratch3 from "./scratch3/index.js"
export default function (window) {
const document = window.document
scratch2.init(window)
scratch3.init(window)
function appendStyles() {
document.head.appendChild(scratch2.makeStyle())
document.head.appendChild(scratch3.makeStyle())
}
function newView(doc, options) {
options = {
style: "scratch2",
...options,
}
options.scale = options.scale || 1
switch (options.style) {
case "scratch2":
return scratch2.newView(doc, options)
case "scratch3":
return scratch3.newView(doc, options)
default:
throw new Error(`Unknown style: ${options.style}`)
}
}
function render(doc, options) {
if (typeof options === "function") {
throw new Error("render() no longer takes a callback")
}
const view = newView(doc, options)
const svg = view.render()
return svg
}
/*****************************************************************************/
/*** Render ***/
// read code from a DOM element
function readCode(el, options) {
options = {
inline: false,
...options,
}
const html = el.innerHTML.replace(/<br>\s?|\n|\r\n|\r/gi, "\n")
const pre = document.createElement("pre")
pre.innerHTML = html
let code = pre.textContent
if (options.inline) {
code = code.replace("\n", "")
}
return code
}
// insert 'svg' into 'el', with appropriate wrapper elements
function replace(el, svg, doc, options) {
let container
if (options.inline) {
container = document.createElement("span")
let cls = "scratchblocks scratchblocks-inline"
if (doc.scripts[0] && !doc.scripts[0].isEmpty) {
cls += ` scratchblocks-inline-${doc.scripts[0].blocks[0].shape}`
}
container.className = cls
container.style.display = "inline-block"
container.style.verticalAlign = "middle"
} else {
container = document.createElement("div")
container.className = "scratchblocks"
}
container.appendChild(svg)
el.innerHTML = ""
el.appendChild(container)
}
/* Render all matching elements in page to shiny scratch blocks.
* Accepts a CSS selector as an argument.
*
* scratchblocks.renderMatching("pre.blocks");
*
* Like the old 'scratchblocks2.parse().
*/
const renderMatching = function (selector, options) {
selector = selector || "pre.blocks"
options = {
// Default values for the options
style: "scratch2",
inline: false,
languages: ["en"],
scale: 1,
read: readCode, // function(el, options) => code
parse: parse, // function(code, options) => doc
render: render, // function(doc) => svg
replace: replace, // function(el, svg, doc, options)
...options,
}
// find elements
const results = [].slice.apply(document.querySelectorAll(selector))
results.forEach(el => {
const code = options.read(el, options)
const doc = options.parse(code, options)
const svg = options.render(doc, options)
options.replace(el, svg, doc, options)
})
}
return {
allLanguages: allLanguages, // read-only
loadLanguages: loadLanguages,
stringify: function (doc) {
return doc.stringify()
},
Label,
Icon,
Input,
Block,
Comment,
Script,
Document,
newView: newView,
read: readCode,
parse: parse,
replace: replace,
render: render,
renderMatching: renderMatching,
appendStyles: appendStyles,
}
}