Some minor bugs

This commit is contained in:
Gregory Schier
2023-03-01 14:16:02 -08:00
parent 915e0e8613
commit 61fe95b300
4 changed files with 18 additions and 29 deletions
-1
View File
@@ -9,7 +9,6 @@ extern crate objc;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::create_dir_all; use std::fs::create_dir_all;
use std::path::Path;
use http::header::{HeaderName, USER_AGENT}; use http::header::{HeaderName, USER_AGENT};
use http::{HeaderMap, HeaderValue, Method}; use http::{HeaderMap, HeaderValue, Method};
+1 -1
View File
@@ -69,7 +69,7 @@ function App() {
<Editor <Editor
valueKey={request.id} valueKey={request.id}
useTemplating useTemplating
defaultValue={request.body ?? undefined} defaultValue={request.body ?? ''}
contentType="application/json" contentType="application/json"
onChange={(body) => updateRequest.mutate({ body })} onChange={(body) => updateRequest.mutate({ body })}
/> />
+2 -3
View File
@@ -119,9 +119,8 @@
background-color: transparent; background-color: transparent;
} }
.cm-editor.cm-focused .cm-activeLineGutter, .cm-editor.cm-focused .cm-activeLineGutter {
.cm-editor.cm-focused .cm-activeLine { color: hsl(var(--color-gray-800));
background-color: hsl(var(--color-gray-100)/0.3);
} }
.cm-editor * { .cm-editor * {
+15 -24
View File
@@ -57,15 +57,17 @@ export default function Editor({
console.log('Failed to initialize Codemirror', e); console.log('Failed to initialize Codemirror', e);
} }
return () => view?.destroy(); return () => view?.destroy();
}, [ref.current]); }, [ref.current, valueKey]);
// Update value when valueKey changes // Update value when valueKey changes
useEffect(() => { // TODO: This would be more efficient but the onChange handler gets fired on update
if (cm === null) return; // useEffect(() => {
cm.view.dispatch({ // if (cm === null) return;
changes: { from: 0, to: cm.view.state.doc.length, insert: `${defaultValue ?? ''}` }, // console.log('NEW DOC', valueKey, defaultValue);
}); // cm.view.dispatch({
}, [valueKey]); // changes: { from: 0, to: cm.view.state.doc.length, insert: `${defaultValue ?? ''}` },
// });
// }, [valueKey]);
// Update language extension when contentType changes // Update language extension when contentType changes
useEffect(() => { useEffect(() => {
@@ -111,31 +113,20 @@ function getExtensions({
), ),
EditorState.transactionFilter.of( EditorState.transactionFilter.of(
(tr: Transaction): TransactionSpec | TransactionSpec[] => { (tr: Transaction): TransactionSpec | TransactionSpec[] => {
if (!tr.isUserEvent('input.paste')) { if (!tr.isUserEvent('input.paste')) return tr;
return tr;
}
// let addedNewline = false;
const trs: TransactionSpec[] = []; const trs: TransactionSpec[] = [];
tr.changes.iterChanges((fromA, toA, fromB, toB, inserted) => { tr.changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
let insert = ''; let insert = '';
for (const line of inserted) { for (const line of inserted) {
insert += line.replace('\n', ''); insert += line.replace('\n', '');
} }
trs.push({ const changes = [{ from: fromB, to: toA, insert }];
...tr, // Update selection now that the text has been changed
selection: undefined, const selection = EditorSelection.create([EditorSelection.cursor(toB - 1)], 0);
changes: [{ from: fromB, to: toA, insert }], trs.push({ ...tr, selection, changes });
});
});
// selection: EditorSelection.create([EditorSelection.cursor(8)], 1),
// console.log('TRS', trs, tr);
trs.push({
selection: EditorSelection.create([EditorSelection.cursor(8)], 1),
}); });
return trs; return trs;
// return addedNewline ? [] : tr;
}, },
), ),
] ]
@@ -144,7 +135,7 @@ function getExtensions({
...(!singleLine ? [multiLineExtensions] : []), ...(!singleLine ? [multiLineExtensions] : []),
...(ext ? [ext] : []), ...(ext ? [ext] : []),
EditorView.updateListener.of((update) => { EditorView.updateListener.of((update) => {
if (typeof onChange === 'function') { if (typeof onChange === 'function' && update.docChanged) {
onChange(update.state.doc.toString()); onChange(update.state.doc.toString());
} }
}), }),