Skip to content

Commit c0e08d8

Browse files
committedFeb 6, 2024
export as snapblocks, rename scratchblocks class
1 parent 3bff9a3 commit c0e08d8

12 files changed

+49
-50
lines changed
 

‎README.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,13 @@ You then need to call `scratchblocks.renderMatching` after the page has loaded.
9292
Make sure this appears at the end of the page (just before the closing `</body>` tag):
9393
```js
9494
<script>
95-
scratchblocks.renderMatching('pre.blocks', {
95+
snapblocks.renderMatching('pre.blocks', {
9696
style: 'snap', // Optional, defaults to 'scratch2'.
9797
languages: ['en', 'de'], // Optional, defaults to ['en'].
9898
scale: 1, // Optional, defaults to 1
9999
});
100100
</script>
101101
```
102-
Note: this does use `scratchblocks` so it's easier to migrate from scratchblocks to snapblocks.
103102

104103
The `renderMatching()` function takes a CSS-style selector for the elements that contain snapblocks code: we use `pre.blocks` to target `pre` tags with the class `blocks`.
105104

@@ -115,9 +114,9 @@ I'm rather fond of the <code class="b">cut from [ v]</code> block in Snap.
115114
To allow this, make a second call to `renderMatching` using the `inline` argument.
116115
```js
117116
<script>
118-
scratchblocks.renderMatching("pre.blocks", ...)
117+
snapblocks.renderMatching("pre.blocks", ...)
119118

120-
scratchblocks.renderMatching("code.b", {
119+
snapblocks.renderMatching("code.b", {
121120
inline: true,
122121
// Repeat `style` and `languages` options here.
123122
});
@@ -139,15 +138,15 @@ The translations files are hundreds of kilobytes in size, so to keep your page b
139138

140139
For example, a translations file that just loads the German language (ISO code `de`) would look something like this:
141140
```js
142-
window.scratchblocks.loadLanguages({
141+
window.snapblocks.loadLanguages({
143142
de: <contents of locales/de.json>
144143
})
145144
```
146145

147146
If you're using a JavaScript bundler you should be able to build your own translations file by calling `require()` with the path to the locale JSON file.
148147
This requires your bundler to allow importing JSON files as JavaScript.
149148
```js
150-
window.scratchblocks.loadLanguages({
149+
window.snapblocks.loadLanguages({
151150
de: require('snapblocks/locales/de.json'),
152151
})
153152
```
@@ -166,14 +165,14 @@ snapblocks.renderMatching('pre.blocks');
166165
```
167166

168167
## ESM Support
169-
Since version 3.6.0, scratchblocks (and subsequently snapblocks) can be properly loaded as an ESM module. The ESM version, instead of defining `window.scratchblocks`, default-exports the `scratchblocks` object. Similarly, the JavaScript translation files default-exports a function to load the translations.
168+
Since version 3.6.0, scratchblocks (and subsequently snapblocks) can be properly loaded as an ESM module. The ESM version, instead of defining `window.snapblocks`, default-exports the `snapblocks` object. Similarly, the JavaScript translation files default-exports a function to load the translations.
170169

171170
```js
172-
import scratchblocks from "./snapblocks-es-min.js";
171+
import snapblocks from "./snapblocks-es-min.js";
173172
import loadTranslations from "./translations-all-es.js";
174-
loadTranslations(scratchblocks);
173+
loadTranslations(snapblocks);
175174

176-
// window.scratchblocks is NOT available!
175+
// window.snapblocks is NOT available!
177176
```
178177

179178
# Languages

‎browser.es.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import init from "./index.js"
22

3-
const scratchblocks = init(window)
3+
const snapblocks = init(window)
44

55
// add our CSS to the page
6-
scratchblocks.appendStyles()
6+
snapblocks.appendStyles()
77

8-
export default scratchblocks
8+
export default snapblocks

‎browser.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import init from "./index.js"
22

3-
const scratchblocks = (window.scratchblocks = init(window))
3+
const snapblocks = (window.snapblocks = init(window))
44

55
// add our CSS to the page
6-
scratchblocks.appendStyles()
6+
snapblocks.appendStyles()
77

8-
export default scratchblocks
8+
export default snapblocks

‎index.html

+19-19
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ <h1>
7979

8080
var chooseLang = document.getElementById('choose-lang');
8181

82-
var languageCodes = Object.keys(scratchblocks.allLanguages)
82+
var languageCodes = Object.keys(snapblocks.allLanguages)
8383
languageCodes.sort()
8484
languageCodes.forEach(function (code) {
8585
if (code === "en") return
8686
var option = document.createElement("option")
8787
option.value = code
8888

89-
var language = scratchblocks.allLanguages[code]
89+
var language = snapblocks.allLanguages[code]
9090
option.textContent = language.name && language.altName ? `${language.name}${language.altName}` : language
9191
.name || language.altName || code
9292
chooseLang.appendChild(option)
@@ -178,16 +178,16 @@ <h1>
178178
/*
179179
// renderMatching API example
180180
preview.innerHTML = obj.script;
181-
scratchblocks.renderMatching('pre.blocks', {
181+
snapblocks.renderMatching('pre.blocks', {
182182
languages: ['en', obj.lang],
183183
render: function(doc, cb) {
184184
doc.render(function(svg) {
185185
var el = document.createElement('div');
186186
el.appendChild(svg);
187187
188188
var link = exportSVGLink = document.createElement('a');
189-
exportLink.href = scratchblocks.exportSVG(svg);
190-
exportLink.setAttribute('download', 'scratchblocks.svg');
189+
exportLink.href = snapblocks.exportSVG(svg);
190+
exportLink.setAttribute('download', 'snapblocks.svg');
191191
exportLink.textContent = "Export SVG";
192192
exportLink.className = 'export-link';
193193
var p = document.createElement('p');
@@ -200,34 +200,34 @@ <h1>
200200
});
201201
*/
202202

203-
var doc = window.doc = scratchblocks.parse(obj.script, {
203+
var doc = window.doc = snapblocks.parse(obj.script, {
204204
languages: obj.lang ? ['en', obj.lang] : ['en'],
205205
});
206206

207-
// doc.translate(scratchblocks.allLanguages['de']);
207+
// doc.translate(snapblocks.allLanguages['de']);
208208

209-
// var doc = window.doc = scratchblocks.fromJSON({
209+
// var doc = window.doc = snapblocks.fromJSON({
210210
// scripts: eval(obj.script),
211211
// });
212212

213-
console.log(scratchblocks.stringify(doc));
214-
var docViewSnap = scratchblocks.newView(doc, {
213+
console.log(snapblocks.stringify(doc));
214+
var docViewSnap = snapblocks.newView(doc, {
215215
style: "snap",
216216
scale: 1
217217
});
218-
var docViewSnapFlat = scratchblocks.newView(doc, {
218+
var docViewSnapFlat = snapblocks.newView(doc, {
219219
style: "snap-flat",
220220
scale: 1
221221
});
222-
var docView2 = scratchblocks.newView(doc, {
222+
var docView2 = snapblocks.newView(doc, {
223223
style: "scratch2",
224224
scale: 1
225225
});
226-
var docView3HighContrast = scratchblocks.newView(doc, {
226+
var docView3HighContrast = snapblocks.newView(doc, {
227227
style: "scratch3-high-contrast",
228228
scale: 0.675
229229
});
230-
var docView3 = scratchblocks.newView(doc, {
230+
var docView3 = snapblocks.newView(doc, {
231231
style: "scratch3",
232232
scale: 0.675
233233
});
@@ -245,8 +245,8 @@ <h1>
245245
svg3HighContrast.classList.add("scratch3");
246246
svg3.classList.add("scratch3")
247247
// Need to add manually if calling View#render (folks, this is why you shouldn't use private API!)
248-
svg3HighContrast.classList.add("scratchblocks-style-scratch3-high-contrast")
249-
svg3.classList.add("scratchblocks-style-scratch3")
248+
svg3HighContrast.classList.add("snapblocks-style-scratch3-high-contrast")
249+
svg3.classList.add("snapblocks-style-scratch3")
250250

251251
const getSelectedView = () => {
252252
switch (exportDropdown.value) {
@@ -266,13 +266,13 @@ <h1>
266266
// add export link
267267
setTimeout(function () {
268268
if (!exportSVGLink) exportSVGLink = document.createElement('a');
269-
exportSVGLink.onclick = () => download("scratchblocks.svg", getSelectedView().exportSVG());
269+
exportSVGLink.onclick = () => download("snapblocks.svg", getSelectedView().exportSVG());
270270
exportSVGLink.textContent = "Export SVG";
271271
exportSVGLink.className = 'export-link';
272272
if (!exportSVGLink.isConnected) document.querySelector('#export-options').appendChild(exportSVGLink);
273273

274274
if (!exportPNGLink) exportPNGLink = document.createElement('a');
275-
exportPNGLink.onclick = () => getSelectedView().exportPNG((url) => download("scratchblocks.png", url), 2);
275+
exportPNGLink.onclick = () => getSelectedView().exportPNG((url) => download("snapblocks.png", url), 2);
276276
exportPNGLink.textContent = "Export PNG";
277277
exportPNGLink.className = 'export-link';
278278
if (!exportPNGLink.isConnected) document.querySelector('#export-options').appendChild(exportPNGLink);
@@ -307,7 +307,7 @@ <h1>
307307
}, 0);
308308

309309
// update language dropdown
310-
var lang = scratchblocks.allLanguages[obj.lang]
310+
var lang = snapblocks.allLanguages[obj.lang]
311311
langStatus.textContent = lang ? `${lang.percentTranslated}%` : "";
312312
}
313313

‎index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default function (window) {
6262
const view = newView(doc, options)
6363
const svg = view.render()
6464
// Used in high contrast theme
65-
svg.classList.add(`scratchblocks-style-${options.style}`)
65+
svg.classList.add(`snapblocks-style-${options.style}`)
6666
return svg
6767
}
6868

@@ -92,16 +92,16 @@ export default function (window) {
9292
let container
9393
if (options.inline) {
9494
container = document.createElement("span")
95-
let cls = "scratchblocks scratchblocks-inline"
95+
let cls = "snapblocks snapblocks-inline"
9696
if (doc.scripts[0] && !doc.scripts[0].isEmpty) {
97-
cls += ` scratchblocks-inline-${doc.scripts[0].blocks[0].shape}`
97+
cls += ` snapblocks-inline-${doc.scripts[0].blocks[0].shape}`
9898
}
9999
container.className = cls
100100
container.style.display = "inline-block"
101101
container.style.verticalAlign = "middle"
102102
} else {
103103
container = document.createElement("div")
104-
container.className = "scratchblocks"
104+
container.className = "snapblocks"
105105
}
106106
container.appendChild(svg)
107107

@@ -112,9 +112,9 @@ export default function (window) {
112112
/* Render all matching elements in page to shiny scratch blocks.
113113
* Accepts a CSS selector as an argument.
114114
*
115-
* scratchblocks.renderMatching("pre.blocks");
115+
* snapblocks.renderMatching("pre.blocks");
116116
*
117-
* Like the old 'scratchblocks2.parse().
117+
* Like the old 'snapblocks2.parse().
118118
*/
119119
const renderMatching = function (selector, options) {
120120
selector = selector || "pre.blocks"

‎locales-src/translations-all-es.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import languages from "../locales/all.js"
2-
export default function init(scratchblocks) {
3-
scratchblocks.loadLanguages(languages)
2+
export default function init(snapblocks) {
3+
snapblocks.loadLanguages(languages)
44
}
55
init.languages = languages

‎locales-src/translations-all.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import languages from "../locales/all.js"
2-
window.scratchblocks.loadLanguages(languages)
2+
window.snapblocks.loadLanguages(languages)

‎locales-src/translations-es.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import languages from "../locales/forums.js"
2-
export default function init(scratchblocks) {
3-
scratchblocks.loadLanguages(languages)
2+
export default function init(snapblocks) {
3+
snapblocks.loadLanguages(languages)
44
}
55
init.languages = languages

‎locales-src/translations.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import languages from "../locales/forums.js"
2-
window.scratchblocks.loadLanguages(languages)
2+
window.snapblocks.loadLanguages(languages)

‎rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default [
5858
output: {
5959
file: pkg.main,
6060
format: "iife",
61-
name: "scratchblocks",
61+
name: "snapblocks",
6262
sourcemap: env.prod,
6363
},
6464
plugins: [

‎scratch3/style.css.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,5 +232,5 @@ const highContrastStyle = {
232232

233233
export default common +
234234
create("", originalStyle) +
235-
create(".scratchblocks-style-scratch3-high-contrast", highContrastStyle) +
235+
create(".snapblocks-style-scratch3-high-contrast", highContrastStyle) +
236236
commonOverride

‎snapshots/headless.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Renderer {
3838
await this.page.goto(
3939
"http://localhost:8002/snapshots/snapshot-testing.html",
4040
)
41-
await this.page.waitForFunction("window.scratchblocksLoaded")
41+
await this.page.waitForFunction("window.snapblocksLoaded")
4242
}
4343

4444
async snapshot(script, options) {

0 commit comments

Comments
 (0)
Please sign in to comment.