-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add persistence to online sandbox editor #323
Changes from 11 commits
51bedc0
fa92ace
5704e9c
1ed362c
9f04b23
3472ecb
c6dd9f9
2731e3e
18b062f
9949b58
478899e
9c60891
16afdf2
d744941
7cf0632
a979d8d
b1d114a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict"; | ||
|
||
module.exports = { presets: ["@babel/preset-env"] }; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import LZString from "../vendor/lz-string/lz-string.js"; | ||
|
||
// The key used to store the sandbox in local/session storage | ||
export const codeStorageKey = `sandbox-code`; | ||
|
||
export const saveSandboxCodeToStorage = (code) => { | ||
localStorage.setItem(codeStorageKey, code); | ||
} | ||
|
||
// The example grammar to use when there is no saved code in the URL or local storage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we have UI for resetting back to this? |
||
export const exampleGrammar = ` | ||
// Simple Arithmetics Grammar | ||
// ========================== | ||
// | ||
// Accepts expressions like "2 * (3 + 4)" and computes their value. | ||
|
||
Expression | ||
= head:Term tail:(_ ("+" / "-") _ Term)* { | ||
return tail.reduce(function(result, element) { | ||
if (element[1] === "+") { return result + element[3]; } | ||
if (element[1] === "-") { return result - element[3]; } | ||
}, head); | ||
} | ||
|
||
Term | ||
= head:Factor tail:(_ ("*" / "/") _ Factor)* { | ||
return tail.reduce(function(result, element) { | ||
if (element[1] === "*") { return result * element[3]; } | ||
if (element[1] === "/") { return result / element[3]; } | ||
}, head); | ||
} | ||
|
||
Factor | ||
= "(" _ expr:Expression _ ")" { return expr; } | ||
/ Integer | ||
|
||
Integer "integer" | ||
= _ [0-9]+ { return parseInt(text(), 10); } | ||
|
||
_ "whitespace" | ||
= [ \\t\\n\\r]* | ||
`; | ||
|
||
/** | ||
* @param {URL} url | ||
* @returns {string | null} | ||
*/ | ||
export function getSandboxInitialContents(url) { | ||
if (url.hash.startsWith("#code/")) { | ||
camchenry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const code = url.hash.substring(6); | ||
const decodedCode = LZString.decompressFromEncodedURIComponent(code); | ||
return decodedCode; | ||
} | ||
|
||
const storedCode = localStorage.getItem(codeStorageKey); | ||
if (storedCode) { | ||
return storedCode; | ||
} | ||
|
||
return exampleGrammar; | ||
} | ||
|
||
/** | ||
* @param {string} code | ||
* @param {URL | string | undefined} baseUrl | ||
* @returns {string} | ||
*/ | ||
export function getEncodedSandboxUrl(code, baseUrl = undefined) { | ||
const encodedCode = LZString.compressToEncodedURIComponent(code); | ||
camchenry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (baseUrl) { | ||
return `${ | ||
typeof baseUrl === "string" ? baseUrl : baseUrl.toString() | ||
}#code/${encodedCode}`; | ||
} else { | ||
return `#code/${encodedCode}`; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,18 @@ | |
"/vendor/codemirror/codemirror.css", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be vendored? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could load it directly from the CDN, but then it wouldn't work in the tests anymore, because we reference the library directly there. I don't believe there is any way in Jest/Node to load from URLs directly, unfortunately. Another advantage of vendoring is that I changed the library to use standard (In the long run, we should just |
||
"/vendor/codemirror/lint.css" | ||
] | ||
modules: [ | ||
"/vendor/lz-string/lz-string.js", | ||
"/js/sandbox.js", | ||
"/js/online.js" | ||
] | ||
scripts: [ | ||
"https://unpkg.com/[email protected]/dist/jquery.min.js", | ||
"https://unpkg.com/[email protected]/dist/FileSaver.min.js", | ||
"https://unpkg.com/[email protected]/dist/inspect.js", | ||
"/vendor/peggy/peggy.min.js", | ||
"/vendor/codemirror/codemirror.js", | ||
"/vendor/codemirror/lint.js", | ||
"/js/online.js" | ||
"/vendor/codemirror/lint.js" | ||
] | ||
--- | ||
|
||
|
@@ -34,43 +38,15 @@ | |
<h2 class="suggestion top"> | ||
<span class="step-number">1</span> | ||
<div class="step-title">Write your Peggy grammar</div> | ||
<button id="copy-link">Copy link</button> | ||
</h2> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td> | ||
<div class="textarea-wrapper"> | ||
<textarea class="code" id="grammar" autocomplete="off" autocorrect="off" | ||
autocapitalize="off" spellcheck="false" disabled>// Simple Arithmetics Grammar | ||
// ========================== | ||
// | ||
// Accepts expressions like "2 * (3 + 4)" and computes their value. | ||
|
||
Expression | ||
= head:Term tail:(_ ("+" / "-") _ Term)* { | ||
return tail.reduce(function(result, element) { | ||
if (element[1] === "+") { return result + element[3]; } | ||
if (element[1] === "-") { return result - element[3]; } | ||
}, head); | ||
} | ||
|
||
Term | ||
= head:Factor tail:(_ ("*" / "/") _ Factor)* { | ||
return tail.reduce(function(result, element) { | ||
if (element[1] === "*") { return result * element[3]; } | ||
if (element[1] === "/") { return result / element[3]; } | ||
}, head); | ||
} | ||
|
||
Factor | ||
= "(" _ expr:Expression _ ")" { return expr; } | ||
/ Integer | ||
|
||
Integer "integer" | ||
= _ [0-9]+ { return parseInt(text(), 10); } | ||
|
||
_ "whitespace" | ||
= [ \t\n\r]*</textarea> | ||
autocapitalize="off" spellcheck="false" disabled></textarea> | ||
</div> | ||
</td> | ||
</tr> | ||
|
@@ -134,4 +110,4 @@ <h2 class="suggestion"> | |
</table> | ||
</td> | ||
</tr> | ||
</table> | ||
</table> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add some tests to the puppeteer tests in the web-test directory as well. |
||
getSandboxInitialContents, | ||
exampleGrammar, | ||
saveSandboxCodeToStorage, | ||
getEncodedSandboxUrl, | ||
} from "../js/sandbox"; | ||
|
||
// Jest can implement localStorage using jsdom, but that is way overkill | ||
// for this simple test suite, so we'll just use a simple in-memory | ||
// mock implementation of local storage for now. | ||
class LocalStorageMock { | ||
store: Record<string, string> = {}; | ||
|
||
clear() { | ||
this.store = {}; | ||
} | ||
|
||
getItem(key: string) { | ||
return this.store[key] || null; | ||
} | ||
|
||
setItem(key: string, value: string) { | ||
this.store[key] = String(value); | ||
} | ||
|
||
removeItem(key: string) { | ||
delete this.store[key]; | ||
} | ||
} | ||
|
||
Object.defineProperty(globalThis, "localStorage", { | ||
value: new LocalStorageMock(), | ||
}); | ||
|
||
describe("getSandboxInitialContents", () => { | ||
it("returns example grammar when nothing is stored locally or in the URL", () => { | ||
const url = new URL("https://peggyjs.org/online"); | ||
expect(getSandboxInitialContents(url)).toEqual(exampleGrammar); | ||
}); | ||
it("returns stored grammar when it is in local storage", () => { | ||
saveSandboxCodeToStorage("stored grammar"); | ||
const url = new URL("https://peggyjs.org/online"); | ||
expect(getSandboxInitialContents(url)).toEqual("stored grammar"); | ||
}); | ||
it("returns grammar stored in URL", () => { | ||
const url = new URL( | ||
"https://peggyjs.org/online#code/OYJwhgthYgBAzgFwPYgKYBNYEsB2sBVAJQBkg" | ||
); | ||
expect(getSandboxInitialContents(url)).toEqual("grammar stored in URL"); | ||
}); | ||
it("returns grammar in URL over stored grammar", () => { | ||
const url = new URL( | ||
"https://peggyjs.org/online#code/OYJwhgthYgBAzgFwPYgKYBNYEsB2sBVAJQBkg" | ||
); | ||
saveSandboxCodeToStorage("stored grammar"); | ||
expect(getSandboxInitialContents(url)).toEqual("grammar stored in URL"); | ||
}); | ||
}); | ||
|
||
describe("getEncodedSandboxUrl", () => { | ||
it("returns just a fragment if there is no base URL", () => { | ||
const grammar = "grammar stored in URL"; | ||
expect(getEncodedSandboxUrl(grammar)).toEqual( | ||
"#code/OYJwhgthYgBAzgFwPYgKYBNYEsB2sBVAJQBkg" | ||
); | ||
}); | ||
it("prepends base URL if it exists", () => { | ||
const grammar = "grammar stored in URL"; | ||
const url = "https://peggyjs.org/online"; | ||
expect(getEncodedSandboxUrl(grammar, url)).toEqual( | ||
"https://peggyjs.org/online#code/OYJwhgthYgBAzgFwPYgKYBNYEsB2sBVAJQBkg" | ||
); | ||
}); | ||
it("works with getSandboxInitialContents", () => { | ||
const baseUrl = new URL("https://peggyjs.org/online"); | ||
const grammar = "grammar which will be stored in URL"; | ||
const url = new URL(getEncodedSandboxUrl(grammar, baseUrl)); | ||
expect(getSandboxInitialContents(url)).toEqual(grammar); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ module.exports = { | |
], | ||
"roots": [ | ||
"<rootDir>/test", | ||
"<rootDir>/docs/test", | ||
camchenry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
"testMatch": [ | ||
"**/*.spec.js", | ||
|
@@ -17,5 +18,9 @@ module.exports = { | |
], | ||
"transform": { | ||
"^.+\\.ts$": "ts-jest", | ||
"^.+\\.js$": "babel-jest", | ||
}, | ||
"transformIgnorePatterns": [ | ||
"/test/cli/fixtures/bad", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jest was trying to parse this file using Babel which was failing, so we ignore it here |
||
], | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This handles transpiling the ESM syntax for the Jest tests