-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathESLintCodeBlock.svelte
84 lines (77 loc) · 1.68 KB
/
ESLintCodeBlock.svelte
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
<script>
import { onMount } from "svelte"
import ESLintEditor from "../eslint/ESLintEditor.svelte"
import {
createLinter,
preprocess,
postprocess,
} from "../eslint/scripts/linter.js"
const linter = createLinter()
let code = ""
export let rules = {}
export let fix = false
export let language = "html"
let time = ""
$: options = {
filename: language === "html" ? "example.svelte" : "example.js",
preprocess,
postprocess,
}
let showDiff = fix
function onLintedResult(evt) {
time = `${evt.detail.time}ms`
}
let slotRoot
onMount(() => {
code = slotRoot.textContent.trim()
})
$: blockHeight = `${Math.max(
120,
20 * (1 + code.split("\n").length) + 100,
)}px`
</script>
<div class="eslint-code-block-default-content" bind:this={slotRoot}>
<slot />
</div>
<div class="eslint-code-block-root" style:height={blockHeight}>
<ESLintEditor
{linter}
bind:code
config={{
parser: language === "html" ? "svelte-eslint-parser" : undefined,
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
rules,
env: {
browser: true,
es2021: true,
},
}}
{language}
{options}
on:result={onLintedResult}
showDiff={showDiff && fix}
/>
<div class="eslint-code-block-tools">
{#if fix}
<label>
<input bind:checked={showDiff} type="checkbox" />
Show Diff
</label>
{/if}
<span style:margin-left="16px">{time}</span>
</div>
</div>
<style>
.eslint-code-block-default-content {
display: none;
}
.eslint-code-block-root {
height: 300px;
}
.eslint-code-block-tools {
text-align: end;
}
</style>