-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement js playground (#243)
* chore: implement state * refactor * implement js playground
- Loading branch information
Showing
11 changed files
with
528 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* @typedef {import("codemirror").Editor} Editor | ||
*/ | ||
import { getInitial } from "./helpers"; | ||
import { Model } from "./model"; | ||
import { View } from "./view"; | ||
|
||
const INITIAL_HTML = /* html */ `<!DOCTYPE html> | ||
<html> | ||
<head> | ||
</head> | ||
<body> | ||
<div> | ||
<li> foo </li> | ||
</div> | ||
</body> | ||
</html> | ||
`; | ||
|
||
const INITAIL_CONFIG = JSON.stringify( | ||
{ rules: { "@html-eslint/indent": "error" } }, | ||
null, | ||
2 | ||
); | ||
|
||
class App { | ||
constructor() { | ||
this.view = new View(); | ||
this.model = new Model(); | ||
this.model.on("lint", () => this.handleLint()); | ||
this.model.on("changeLanguage", () => this.handleLanguageChange()); | ||
this.view.codeEditor.on("change", (editor) => | ||
this.handleCodeChange(editor) | ||
); | ||
this.view.configEditor.on("change", (editor) => { | ||
this.handleConfigChange(editor); | ||
}); | ||
this.view.$languageTabs.addEventListener("click", (event) => { | ||
const $tab = event.target.closest("[data-language]"); | ||
this.model.setLanguage($tab.dataset.language); | ||
}); | ||
} | ||
|
||
start() { | ||
const decoded = decodeURIComponent( | ||
escape(window.atob(window.location.hash.substring(1))) | ||
); | ||
this.model.load(decoded); | ||
|
||
this.view.codeEditor.setValue(this.model.getCode()); | ||
this.view.configEditor.setValue( | ||
JSON.stringify({ rules: this.model.rules }, null, 2) | ||
); | ||
this.handleCodeChange(this.view.codeEditor); | ||
this.handleConfigChange(this.view.configEditor); | ||
this.handleLanguageChange(); | ||
} | ||
|
||
/** | ||
* @param {Editor} editor | ||
*/ | ||
handleConfigChange(editor) { | ||
try { | ||
const rules = JSON.parse(editor.getValue()).rules; | ||
this.model.setRules(rules); | ||
this.model.lint(); | ||
} catch (e) { | ||
this.view.renderErrors([ | ||
{ | ||
line: 0, | ||
column: 0, | ||
message: e.message, | ||
fatal: true, | ||
}, | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* @param {Editor} editor | ||
*/ | ||
handleCodeChange(editor) { | ||
this.model.setCode(editor.getValue()); | ||
this.model.lint(); | ||
} | ||
|
||
handleLint() { | ||
this.view.renderErrors(this.model.messages); | ||
this.view.renderFixed(this.model.fixed); | ||
const hash = this.model.toHash(); | ||
window.location.hash = hash; | ||
} | ||
|
||
handleLanguageChange() { | ||
this.view.renderLanguageTabs(this.model.language.value); | ||
this.view.codeEditor.setOption("mode", this.model.language.mime()); | ||
this.view.codeEditor.setValue(this.model.getCode()); | ||
this.model.lint(); | ||
} | ||
} | ||
|
||
const app = new App(); | ||
app.start(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @typedef {import('eslint').Linter.LintMessage} LintMessage | ||
* @typedef {import("codemirror").Position} Position | ||
*/ | ||
|
||
import { Language } from "./language"; | ||
|
||
/** | ||
* @param {number} pos | ||
* @returns {number} | ||
*/ | ||
function toMarkerPos(pos) { | ||
return pos - 1; | ||
} | ||
|
||
/** | ||
* @param {LintMessage} message | ||
* @returns {[Position, Position]} | ||
*/ | ||
export function toMarker(message) { | ||
const from = { | ||
line: toMarkerPos(message.line), | ||
ch: toMarkerPos(message.column), | ||
}; | ||
const to = { | ||
line: toMarkerPos(message.endLine || message.line), | ||
ch: toMarkerPos(message.endColumn || message.column), | ||
}; | ||
return [from, to]; | ||
} | ||
|
||
export const INITIAL_HTML = /* html */ `<!DOCTYPE html> | ||
<html> | ||
<head> | ||
</head> | ||
<body> | ||
<div> | ||
<li> foo </li> | ||
</div> | ||
</body> | ||
</html> | ||
`; | ||
|
||
export const INITIAL_JAVASCRIPT = `html\` | ||
<div> | ||
<span> | ||
</span> | ||
</div> | ||
\` | ||
const html = /*html*/\` | ||
<div> | ||
<span> | ||
</span> | ||
</div> | ||
\`;`; | ||
|
||
export const INITAIL_CONFIG = JSON.stringify( | ||
{ rules: { "@html-eslint/indent": "error" } }, | ||
null, | ||
2 | ||
); | ||
|
||
/** | ||
* @param {string} language | ||
*/ | ||
export function getInitialCode(language) { | ||
if (language === "javascript") { | ||
return INITIAL_JAVASCRIPT; | ||
} | ||
return INITIAL_HTML; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export class Language { | ||
/** | ||
* @param {"html" | "javascript"} language | ||
*/ | ||
constructor(language) { | ||
this.value = language; | ||
} | ||
|
||
mime() { | ||
return `text/${this.value}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.