|
|
@@ -20,99 +20,8 @@
|
|
|
import Highlight, { LineNumbers } from "svelte-highlight";
|
|
|
import json from "svelte-highlight/languages/json";
|
|
|
import { atelierForest } from "svelte-highlight/styles";
|
|
|
- import { json as cmJson } from "@codemirror/lang-json";
|
|
|
- import { EditorView, basicSetup } from "codemirror";
|
|
|
- // import { dracula, coolGlow } from "thememirror";
|
|
|
- import { onMount } from "svelte";
|
|
|
- import {
|
|
|
- BrushCleaning,
|
|
|
- Clipboard,
|
|
|
- FastForward,
|
|
|
- Loader,
|
|
|
- PlusIcon,
|
|
|
- TrashIcon,
|
|
|
- } from "@lucide/svelte";
|
|
|
- import { vim } from "@replit/codemirror-vim";
|
|
|
- import { EditorState, StateEffect } from "@codemirror/state";
|
|
|
- import {
|
|
|
- lineNumbersRelative,
|
|
|
- stateChangeListener,
|
|
|
- } from "$lib/codemirror/lines";
|
|
|
-
|
|
|
- let view: EditorView;
|
|
|
-
|
|
|
- const vimExtension = vim();
|
|
|
- const relativeLines = lineNumbersRelative();
|
|
|
- let vimEnabled = $state(true);
|
|
|
- const jsonExt = cmJson();
|
|
|
-
|
|
|
- let editorState = EditorState.create({
|
|
|
- doc: _state.entry.body?.body ?? "",
|
|
|
- extensions: [
|
|
|
- basicSetup,
|
|
|
- jsonExt,
|
|
|
- vimExtension,
|
|
|
- relativeLines,
|
|
|
- stateChangeListener((update) => {
|
|
|
- console.log(update);
|
|
|
- if (update.docChanged) {
|
|
|
- updateBodyContent(update.state.doc.toString(), "Json");
|
|
|
- }
|
|
|
- }),
|
|
|
- ],
|
|
|
- });
|
|
|
-
|
|
|
- onMount(() => {
|
|
|
- view = new EditorView({
|
|
|
- parent: document.querySelector("#editor"),
|
|
|
- state: editorState,
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- function toggleVim() {
|
|
|
- vimEnabled = !vimEnabled;
|
|
|
-
|
|
|
- const exts = [basicSetup, jsonExt];
|
|
|
-
|
|
|
- if (vimEnabled) {
|
|
|
- exts.push(vimExtension);
|
|
|
- exts.push(relativeLines);
|
|
|
- }
|
|
|
-
|
|
|
- view.dispatch({
|
|
|
- effects: StateEffect.reconfigure.of([exts]),
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- async function copyContent() {
|
|
|
- const content = view.state.doc.toString();
|
|
|
- await navigator.clipboard.writeText(content);
|
|
|
- }
|
|
|
-
|
|
|
- async function formatContent() {
|
|
|
- const content = view.state.doc.toString();
|
|
|
-
|
|
|
- view.dispatch({
|
|
|
- changes: {
|
|
|
- from: 0,
|
|
|
- to: view.state.doc.length,
|
|
|
- insert: JSON.stringify(JSON.parse(content), null, 2),
|
|
|
- },
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // $effect(() => {
|
|
|
- // if (_state.entry.body !== null) {
|
|
|
- // console.log("triggering effect editor dispatch");
|
|
|
- // view.dispatch({
|
|
|
- // changes: {
|
|
|
- // from: 0,
|
|
|
- // to: view.state.doc.length,
|
|
|
- // insert: _state.entry.body.body,
|
|
|
- // },
|
|
|
- // });
|
|
|
- // }
|
|
|
- // });
|
|
|
+ import { Loader, PlusIcon, TrashIcon } from "@lucide/svelte";
|
|
|
+ import CodeMirror from "./CodeMirror.svelte";
|
|
|
|
|
|
let isSending = $state(false);
|
|
|
let response: any = $state();
|
|
|
@@ -137,10 +46,10 @@
|
|
|
isSending = true;
|
|
|
try {
|
|
|
response = await sendRequest();
|
|
|
- console.timeEnd("request");
|
|
|
} catch (e) {
|
|
|
console.error("error sending request", e);
|
|
|
} finally {
|
|
|
+ console.timeEnd("request");
|
|
|
isSending = false;
|
|
|
}
|
|
|
}
|
|
|
@@ -405,20 +314,7 @@
|
|
|
<Tabs.Trigger value="none" onclick={() => deleteBody()}
|
|
|
>None</Tabs.Trigger
|
|
|
>
|
|
|
- <Tabs.Trigger
|
|
|
- value="json"
|
|
|
- onclick={() =>
|
|
|
- updateBodyContent("", "Json").then(() => {
|
|
|
- console.log("triggering effect editor dispatch");
|
|
|
- view.dispatch({
|
|
|
- changes: {
|
|
|
- from: 0,
|
|
|
- to: view.state.doc.length,
|
|
|
- insert: _state.entry.body.body,
|
|
|
- },
|
|
|
- });
|
|
|
- })}>JSON</Tabs.Trigger
|
|
|
- >
|
|
|
+ <Tabs.Trigger value="json">JSON</Tabs.Trigger>
|
|
|
<Tabs.Trigger value="form">Form</Tabs.Trigger>
|
|
|
<Tabs.Trigger value="text">Text</Tabs.Trigger>
|
|
|
</Tabs.List>
|
|
|
@@ -426,38 +322,18 @@
|
|
|
<Tabs.Content value="none">No body</Tabs.Content>
|
|
|
|
|
|
<Tabs.Content value="json">
|
|
|
- <div class="w-full flex rounded-md border max-h-60">
|
|
|
- <!-- EDITOR SIDEBAR -->
|
|
|
-
|
|
|
- <div class="sticky flex flex-col gap-2 p-2 border-r">
|
|
|
- <Button
|
|
|
- onclick={toggleVim}
|
|
|
- variant={vimEnabled ? "default" : "outline"}
|
|
|
- size="icon-sm"
|
|
|
- >
|
|
|
- <FastForward />
|
|
|
- </Button>
|
|
|
-
|
|
|
- <Button
|
|
|
- onclick={copyContent}
|
|
|
- variant="outline"
|
|
|
- size="icon-sm"><Clipboard /></Button
|
|
|
- >
|
|
|
-
|
|
|
- <Button
|
|
|
- onclick={formatContent}
|
|
|
- variant="outline"
|
|
|
- size="icon-sm"><BrushCleaning /></Button
|
|
|
- >
|
|
|
- </div>
|
|
|
-
|
|
|
- <!-- EDITOR -->
|
|
|
-
|
|
|
- <div
|
|
|
- id="editor"
|
|
|
- class="mx-auto w-full rounded-md overflow-scroll max-h-fit"
|
|
|
- ></div>
|
|
|
- </div>
|
|
|
+ <CodeMirror
|
|
|
+ input={_state.entry.body?.body}
|
|
|
+ onStateChange={(update) => {
|
|
|
+ // console.log(update);
|
|
|
+ if (
|
|
|
+ update.docChanged &&
|
|
|
+ _state.entry!!.body?.body !== update.state.doc.toString()
|
|
|
+ ) {
|
|
|
+ updateBodyContent(update.state.doc.toString(), "Json");
|
|
|
+ }
|
|
|
+ }}
|
|
|
+ />
|
|
|
</Tabs.Content>
|
|
|
|
|
|
<Tabs.Content value="form">
|