Skip to content

Commit 1086943

Browse files
committed
update demo
1 parent 62eac20 commit 1086943

File tree

2 files changed

+117
-16
lines changed

2 files changed

+117
-16
lines changed

explorer-v2/src/lib/ESLintEditor.svelte

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
export let code = '';
1010
export let config = {};
1111
export let options = {};
12+
export let fix = true;
1213
1314
let fixedValue = code;
1415
let leftMarkers = [];
1516
let rightMarkers = [];
1617
1718
let messageMap = new Map();
1819
20+
// eslint-disable-next-line no-use-before-define -- TODO
21+
$: showApplyFix = fix && fixedValue !== code;
1922
$: {
2023
lint(linter, code, config, options);
2124
}
@@ -56,6 +59,9 @@
5659
rightMarkers = await Promise.all(fixResult.messages.map((m) => messageToMarker(m)));
5760
}
5861
62+
function applyFix() {
63+
code = fixedValue;
64+
}
5965
/** message to marker */
6066
async function messageToMarker(message, messageMap) {
6167
const monaco = await loadMonacoEditor();
@@ -186,14 +192,47 @@
186192
}
187193
</script>
188194

189-
<MonacoEditor
190-
bind:code
191-
bind:rightCode={fixedValue}
192-
language="html"
193-
diffEditor
194-
markers={leftMarkers}
195-
{rightMarkers}
196-
{provideCodeActions}
197-
/>
198-
199-
<style></style>
195+
<div class="eslint-editor">
196+
<MonacoEditor
197+
bind:code
198+
bind:rightCode={fixedValue}
199+
language="html"
200+
diffEditor={fix}
201+
markers={leftMarkers}
202+
{rightMarkers}
203+
{provideCodeActions}
204+
/>
205+
<div class="eslint-editor__tools">
206+
{#if showApplyFix}
207+
<button on:click={applyFix}>Apply Fix</button>
208+
{/if}
209+
</div>
210+
</div>
211+
212+
<style>
213+
.eslint-editor {
214+
height: 100%;
215+
position: relative;
216+
}
217+
.eslint-editor__tools {
218+
display: flex;
219+
height: 42px;
220+
position: absolute;
221+
right: 16px;
222+
bottom: 16px;
223+
padding: 8px;
224+
}
225+
.eslint-editor__tools > button {
226+
cursor: pointer;
227+
background-color: transparent;
228+
color: #ddd;
229+
border: solid #ddd 1px;
230+
border-radius: 4px;
231+
outline: none;
232+
padding: 0 16px;
233+
appearance: none;
234+
}
235+
.eslint-editor__tools > button:hover {
236+
background-color: rgba(255, 255, 255, 0.2);
237+
}
238+
</style>

explorer-v2/src/lib/MonacoEditor.svelte

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
export let rightMarkers = [];
1414
export let provideCodeActions = null;
1515
16+
export let waiting = null;
1617
let rootElement,
1718
editor,
1819
setLeftValue,
@@ -21,6 +22,9 @@
2122
setRightMarkers,
2223
getLeftEditor,
2324
codeActionProviderDisposable;
25+
const loadingMonaco = loadMonacoEditor();
26+
// eslint-disable-next-line no-use-before-define -- TODO
27+
$: loading = Promise.all([waiting, loadingMonaco]);
2428
$: {
2529
if (setLeftValue) {
2630
setLeftValue(code);
@@ -44,7 +48,7 @@
4448
$: {
4549
disposeCodeActionProvider();
4650
if (provideCodeActions) {
47-
loadMonacoEditor().then((monaco) => {
51+
loadingMonaco.then((monaco) => {
4852
codeActionProviderDisposable = monaco.languages.registerCodeActionProvider(language, {
4953
provideCodeActions(model, range, context) {
5054
const editor = getLeftEditor?.();
@@ -63,8 +67,11 @@
6367
}
6468
}
6569
70+
let started = false;
6671
onMount(async () => {
67-
const monaco = await loadMonacoEditor();
72+
started = true;
73+
await loading;
74+
const monaco = await loadingMonaco;
6875
const options = {
6976
value: code,
7077
readOnly,
@@ -172,7 +179,7 @@
172179
}
173180
}
174181
async function updateMarkers(editor, markers) {
175-
const monaco = await loadMonacoEditor();
182+
const monaco = await loadingMonaco;
176183
const model = editor.getModel();
177184
const id = editor.getId();
178185
monaco.editor.setModelMarkers(model, id, JSON.parse(JSON.stringify(markers)));
@@ -206,13 +213,68 @@
206213
codeActionProviderDisposable.dispose();
207214
}
208215
}
216+
217+
function typewriter(node, { speed = 50 }) {
218+
const valid =
219+
node.childNodes.length === 0 ||
220+
(node.childNodes.length === 1 && node.childNodes[0].nodeType === Node.TEXT_NODE);
221+
222+
if (!valid) {
223+
throw new Error(`This transition only works on elements with a single text node child`);
224+
}
225+
226+
const texts = node.textContent.split(/(?=\S)/);
227+
const duration = texts.length * speed;
228+
229+
return {
230+
duration,
231+
tick: (t) => {
232+
const i = ~~(texts.length * t);
233+
node.textContent = texts.slice(0, i).join('');
234+
}
235+
};
236+
}
209237
</script>
210238
211-
<div bind:this={rootElement} class="root" />
239+
{#await loading}
240+
{#if started}
241+
{#if diffEditor}
242+
<div
243+
class="eslint-editor-monaco-root eslint-editor-monaco-root--wait eslint-editor-monaco-root__flex"
244+
>
245+
<pre in:typewriter>Loading...</pre>
246+
<pre in:typewriter>Loading...</pre>
247+
</div>
248+
{:else}
249+
<pre
250+
class="eslint-editor-monaco-root eslint-editor-monaco-root--wait"
251+
in:typewriter>Loading...</pre>
252+
{/if}
253+
{/if}
254+
{:then _}
255+
<div bind:this={rootElement} class="eslint-editor-monaco-root" />
256+
{/await}
212257
213258
<style>
214-
.root {
259+
.eslint-editor-monaco-root {
215260
width: 100%;
216261
height: 100%;
217262
}
263+
264+
.eslint-editor-monaco-root--wait {
265+
color: #9cdcfe;
266+
border: 1px solid #cfd4db;
267+
background-color: #282c34;
268+
font-family: Menlo, Monaco, 'Courier New', monospace;
269+
font-size: 14px;
270+
line-height: 21px;
271+
padding-left: 52px;
272+
}
273+
.eslint-editor-monaco-root__flex {
274+
display: flex;
275+
}
276+
.eslint-editor-monaco-root__flex > * {
277+
height: 100%;
278+
width: 50%;
279+
}
218280
</style>

0 commit comments

Comments
 (0)