-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
4 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,12 @@ | ||
import { HtmlBuilder } from './html-builder' | ||
|
||
describe('HtmlBuilder', () => { | ||
it('build', () => { | ||
const html = HtmlBuilder.build((b) => { | ||
b.html({ | ||
lang: 'en', | ||
}) | ||
}) | ||
expect(html).toBe('<!DOCTYPE html><html lang="en"></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,78 @@ | ||
type Attributes = { [key: string]: string } | ||
type Block = (builder: HtmlBuilder) => void | ||
const emptyBlock: Block = () => {} | ||
|
||
export class HtmlBuilder { | ||
public static build(block: (builder: HtmlBuilder) => void): string { | ||
const builder = new HtmlBuilder() | ||
builder.result += '<!DOCTYPE html>' | ||
block(builder) | ||
return builder.result | ||
} | ||
|
||
public element(tag: string, attributes: Attributes, block: Block = emptyBlock) { | ||
this.result += '<' | ||
this.result += tag | ||
|
||
for (const [key, value] of Object.entries(attributes)) { | ||
this.result += ' ' | ||
this.result += key | ||
if (value.length > 0) { | ||
this.result += '=' | ||
this.result += '"' | ||
this.result += value | ||
this.result += '"' | ||
} | ||
} | ||
|
||
this.result += '>' | ||
|
||
if (block) { | ||
block(this) | ||
} | ||
|
||
this.result += '</' | ||
this.result += tag | ||
this.result += '>' | ||
} | ||
|
||
public body(attributes: Attributes, block: Block = emptyBlock) { | ||
this.element('body', attributes, block) | ||
} | ||
|
||
public html(attributes: Attributes, block: Block = emptyBlock) { | ||
this.element('html', attributes, block) | ||
} | ||
|
||
public head(attributes: Attributes, block: Block = emptyBlock) { | ||
this.element('head', attributes, block) | ||
} | ||
|
||
public meta(attributes: Attributes, block: Block = emptyBlock) { | ||
this.element('meta', attributes, block) | ||
} | ||
|
||
public title(attributes: Attributes, block: Block = emptyBlock) { | ||
this.element('title', attributes, block) | ||
} | ||
|
||
public img(attributes: Attributes, block: Block = emptyBlock) { | ||
this.element('img', attributes, block) | ||
} | ||
|
||
public input(attributes: Attributes, block: Block = emptyBlock) { | ||
this.element('input', attributes, block) | ||
} | ||
|
||
public h1(attributes: Attributes, block: Block = emptyBlock) { | ||
this.element('h1', attributes, block) | ||
} | ||
|
||
public text(value: string) { | ||
this.result += value | ||
} | ||
|
||
private result: string = '' | ||
|
||
private constructor() {} | ||
} |
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,26 @@ | ||
import * as vscode from 'vscode' | ||
import { HtmlBuilder } from './html-builder' | ||
|
||
export function showRunner() { | ||
const panel = vscode.window.createWebviewPanel('sksl.runner', 'SkSL Runner', vscode.ViewColumn.Beside, {}) | ||
panel.webview.html = getWebviewContent() | ||
} | ||
|
||
function getWebviewContent() { | ||
return HtmlBuilder.build((_) => { | ||
_.html({ lang: 'en' }, () => { | ||
_.head({}, () => { | ||
_.meta({ charset: 'UTF-8' }) | ||
_.meta({ name: 'viewport', content: 'width=device-width, initial-scale=1.0' }) | ||
_.title({}, () => { | ||
_.text('SkSL Runner') | ||
}) | ||
}) | ||
_.body({}, () => { | ||
_.h1({}, () => { | ||
_.text('SkSL Runner') | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |