Compare commits

...

5 commits

Author SHA1 Message Date
Lukáš Ondrejka
83b5c3888b
Merge d51cc39711 into a65a9f5549 2025-01-01 01:47:19 +01:00
turnah
a65a9f5549
fix bug 176: preserve YAML comments when reordering items by matching… (#685)
Some checks failed
Node.js CI - Dockge / ci (22, ARM) (push) Has been cancelled
Node.js CI - Dockge / ci (22, ARM64) (push) Has been cancelled
Node.js CI - Dockge / ci (22, macos-latest) (push) Has been cancelled
Node.js CI - Dockge / ci (22, ubuntu-latest) (push) Has been cancelled
Node.js CI - Dockge / ci (22, windows-latest) (push) Has been cancelled
json-yaml-validate / json-yaml-validate (push) Has been cancelled
2024-12-31 15:43:17 +08:00
Cyril59310
9b73e44cd9
Remove useless scrollbar (#642) 2024-12-31 15:41:15 +08:00
Lukáš Ondrejka
d51cc39711 Fix removeInput 2024-10-13 19:46:52 +02:00
Lukáš Ondrejka
48ccc27b9f Implement RIGHT and LEFT keys for cursor navigation in terminal 2024-10-13 19:12:10 +02:00
2 changed files with 65 additions and 34 deletions

View file

@ -236,42 +236,63 @@ export function copyYAMLComments(doc : Document, src : Document) {
/** /**
* Copy yaml comments from srcItems to items * Copy yaml comments from srcItems to items
* Typescript is super annoying here, so I have to use any here * Attempts to preserve comments by matching content rather than just array indices
* TODO: Since comments are belong to the array index, the comments will be lost if the order of the items is changed or removed or added.
*/ */
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
function copyYAMLCommentsItems(items : any, srcItems : any) { function copyYAMLCommentsItems(items: any, srcItems: any) {
if (!items || !srcItems) { if (!items || !srcItems) {
return; return;
} }
// First pass - try to match items by their content
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const item : any = items[i]; const item: any = items[i];
// Try to find matching source item by content
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const srcItem : any = srcItems[i]; const srcIndex = srcItems.findIndex((srcItem: any) =>
JSON.stringify(srcItem.value) === JSON.stringify(item.value) &&
JSON.stringify(srcItem.key) === JSON.stringify(item.key)
);
if (!srcItem) { if (srcIndex !== -1) {
continue; // eslint-disable-next-line @typescript-eslint/no-explicit-any
} const srcItem: any = srcItems[srcIndex];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const nextSrcItem: any = srcItems[srcIndex + 1];
if (item.key && srcItem.key) { if (item.key && srcItem.key) {
item.key.comment = srcItem.key.comment; item.key.comment = srcItem.key.comment;
item.key.commentBefore = srcItem.key.commentBefore; item.key.commentBefore = srcItem.key.commentBefore;
} }
if (srcItem.comment) { if (srcItem.comment) {
item.comment = srcItem.comment; item.comment = srcItem.comment;
} }
if (item.value && srcItem.value) { // Handle comments between array items
if (typeof item.value === "object" && typeof srcItem.value === "object") { if (nextSrcItem && nextSrcItem.commentBefore) {
item.value.comment = srcItem.value.comment; if (items[i + 1]) {
item.value.commentBefore = srcItem.value.commentBefore; items[i + 1].commentBefore = nextSrcItem.commentBefore;
}
}
if (item.value.items && srcItem.value.items) { // Handle trailing comments after array items
copyYAMLCommentsItems(item.value.items, srcItem.value.items); if (srcItem.value && srcItem.value.comment) {
if (item.value) {
item.value.comment = srcItem.value.comment;
}
}
if (item.value && srcItem.value) {
if (typeof item.value === "object" && typeof srcItem.value === "object") {
item.value.comment = srcItem.value.comment;
item.value.commentBefore = srcItem.value.commentBefore;
if (item.value.items && srcItem.value.items) {
copyYAMLCommentsItems(item.value.items, srcItem.value.items);
}
} }
} }
} }

View file

@ -154,16 +154,17 @@ export default {
}, },
removeInput() { removeInput() {
const textAfterCursorLength = this.terminalInputBuffer.length - this.cursorPosition;
const spaces = " ".repeat(textAfterCursorLength);
const backspaceCount = this.terminalInputBuffer.length; const backspaceCount = this.terminalInputBuffer.length;
const backspaces = "\b \b".repeat(backspaceCount); const backspaces = "\b \b".repeat(backspaceCount);
this.cursorPosition = 0; this.cursorPosition = 0;
this.terminal.write(backspaces); this.terminal.write(spaces + backspaces);
this.terminalInputBuffer = ""; this.terminalInputBuffer = "";
}, },
mainTerminalConfig() { mainTerminalConfig() {
this.terminal.onKey(e => { this.terminal.onKey(e => {
const code = e.key.charCodeAt(0);
console.debug("Encode: " + JSON.stringify(e.key)); console.debug("Encode: " + JSON.stringify(e.key));
if (e.key === "\r") { if (e.key === "\r") {
@ -180,29 +181,39 @@ export default {
this.$root.emitAgent(this.endpoint, "terminalInput", this.name, buffer + e.key, (err) => { this.$root.emitAgent(this.endpoint, "terminalInput", this.name, buffer + e.key, (err) => {
this.$root.toastError(err.msg); this.$root.toastError(err.msg);
}); });
} else if (e.key === "\u007F") { // Backspace
} else if (code === 127) { // Backspace
if (this.cursorPosition > 0) { if (this.cursorPosition > 0) {
this.terminal.write("\b \b"); const trimmedTextBeforeCursor = this.terminalInputBuffer.slice(0, this.cursorPosition - 1);
const textAfterCursor = this.terminalInputBuffer.slice(this.cursorPosition);
const clearAfterCursor = " ".repeat(textAfterCursor.length) + "\b \b".repeat(textAfterCursor.length + 1);
this.terminalInputBuffer = trimmedTextBeforeCursor + textAfterCursor;
this.terminal.write(clearAfterCursor + textAfterCursor + "\b".repeat(textAfterCursor.length));
this.cursorPosition--; this.cursorPosition--;
this.terminalInputBuffer = this.terminalInputBuffer.slice(0, -1);
} }
} else if (e.key === "\u001B\u005B\u0041" || e.key === "\u001B\u005B\u0042") { // UP OR DOWN } else if (e.key === "\u001B\u005B\u0041" || e.key === "\u001B\u005B\u0042") { // UP OR DOWN
// Do nothing // Do nothing
} else if (e.key === "\u001B\u005B\u0043") { // RIGHT } else if (e.key === "\u001B\u005B\u0043") { // RIGHT
// TODO if (this.cursorPosition < this.terminalInputBuffer.length) {
this.terminal.write(this.terminalInputBuffer[this.cursorPosition]);
this.cursorPosition++;
}
} else if (e.key === "\u001B\u005B\u0044") { // LEFT } else if (e.key === "\u001B\u005B\u0044") { // LEFT
// TODO if (this.cursorPosition > 0) {
this.terminal.write("\b");
this.cursorPosition--;
}
} else if (e.key === "\u0003") { // Ctrl + C } else if (e.key === "\u0003") { // Ctrl + C
console.debug("Ctrl + C"); console.debug("Ctrl + C");
this.$root.emitAgent(this.endpoint, "terminalInput", this.name, e.key); this.$root.emitAgent(this.endpoint, "terminalInput", this.name, e.key);
this.removeInput(); this.removeInput();
} else if (e.key === "\u0009" || e.key.startsWith("\u001B")) { // TAB or other special keys
// Do nothing
} else { } else {
const textBeforeCursor = this.terminalInputBuffer.slice(0, this.cursorPosition);
const textAfterCursor = this.terminalInputBuffer.slice(this.cursorPosition);
this.terminalInputBuffer = textBeforeCursor + e.key + textAfterCursor;
this.terminal.write(e.key + textAfterCursor + "\b".repeat(textAfterCursor.length));
this.cursorPosition++; this.cursorPosition++;
this.terminalInputBuffer += e.key;
console.log(this.terminalInputBuffer);
this.terminal.write(e.key);
} }
}); });
}, },
@ -247,7 +258,6 @@ export default {
<style scoped lang="scss"> <style scoped lang="scss">
.main-terminal { .main-terminal {
height: 100%; height: 100%;
overflow-x: scroll;
} }
</style> </style>