An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Outdent the current code block line with Shift+Tab https://github.com/siyuan-note/siyuan/issues/19668
Danielc565d39bf133
diff --git a/app/src/protyle/wysiwyg/codeBlock.ts b/app/src/protyle/wysiwyg/codeBlock.tsindex 4731c955af..08525e1d3e 100644--- a/app/src/protyle/wysiwyg/codeBlock.ts+++ b/app/src/protyle/wysiwyg/codeBlock.ts@@ -3,7 +3,7 @@ import {focusByOffset, getSelectionOffset, getUndoFocusContext, setLastNodeRangeimport {updateTransaction} from "./transaction";import {Constants} from "../../constants";import {- getCodeBlockDeleteStart,+ getCodeBlockOutdentRange,getCodeBlockLineRange,getCodeTabSpace,resolveCodeTabSpaces,@@ -27,16 +27,16 @@ export const tabCodeBlock = (protyle: IProtyle, nodeElement: HTMLElement,if (range.collapsed) {if (outdent) {const caret = getSelectionOffset(editableElement, undefined, range).start;- const deleteStart = getCodeBlockDeleteStart(editableElement.textContent, caret, tabSpace);- if (deleteStart === caret) {+ const outdentRange = getCodeBlockOutdentRange(editableElement.textContent, caret, tabSpace);+ if (outdentRange.start === outdentRange.end) {return;}- const deleteRange = focusByOffset(editableElement, deleteStart, caret, false) as Range;+ const deleteRange = focusByOffset(editableElement, outdentRange.start, outdentRange.end, false) as Range;if (!deleteRange) {return;}deleteRange.deleteContents();- const caretRange = focusByOffset(editableElement, deleteStart, deleteStart, false) as Range;+ const caretRange = focusByOffset(editableElement, outdentRange.caret, outdentRange.caret, false) as Range;if (!caretRange) {return;}diff --git a/app/src/protyle/wysiwyg/codeBlockUtil.test.ts b/app/src/protyle/wysiwyg/codeBlockUtil.test.tsindex a537947ae3..36e17e8a8a 100644--- a/app/src/protyle/wysiwyg/codeBlockUtil.test.ts+++ b/app/src/protyle/wysiwyg/codeBlockUtil.test.ts@@ -1,7 +1,7 @@import * as assert from "node:assert/strict";import test from "node:test";import {- getCodeBlockDeleteStart,+ getCodeBlockOutdentRange,getCodeBlockLineRange,getCodeTabSpace,resolveCodeTabSpaces,@@ -36,11 +36,36 @@ test("code block line indentation handles spaces, tabs, partial indentation andassert.equal(updateCodeBlockLines(" one\n\ttwo", "\t", true), " one\ntwo");});-test("collapsed code block outdent deletes only the preceding indentation unit", () => {- assert.equal(getCodeBlockDeleteStart(" value", 4, " "), 0);- assert.equal(getCodeBlockDeleteStart(" value", 2, " "), 0);- assert.equal(getCodeBlockDeleteStart("value ", 9, " "), 5);- assert.equal(getCodeBlockDeleteStart("\tvalue", 1, " "), 0);- assert.equal(getCodeBlockDeleteStart(" value", 2, "\t"), 2);- assert.equal(getCodeBlockDeleteStart("line\n value", 9, " "), 5);+const outdentAtCaret = (text: string, caret: number, tabSpace = " ") => {+ const range = getCodeBlockOutdentRange(text, caret, tabSpace);+ return {text: text.substring(0, range.start) + text.substring(range.end), caret: range.caret};+};++test("collapsed code block outdent removes leading indentation from any caret position", () => {+ for (const [caret, expected] of [[0, 0], [2, 0], [4, 0], [6, 2], [9, 5]]) {+ assert.deepEqual(outdentAtCaret(" value", caret), {text: "value", caret: expected});+ }+ assert.deepEqual(outdentAtCaret(" value", 5), {text: " value", caret: 1});+ assert.deepEqual(outdentAtCaret(" value", 7), {text: "value", caret: 5});+ assert.deepEqual(outdentAtCaret(" value", 9, " "), {text: " value", caret: 7});+});++test("collapsed code block outdent uses the configured unit and preserves other whitespace", () => {+ assert.deepEqual(outdentAtCaret("\tvalue", 6), {text: "value", caret: 5});+ assert.deepEqual(outdentAtCaret("\t\tvalue", 7, "\t"), {text: "\tvalue", caret: 6});+ assert.deepEqual(outdentAtCaret(" value", 7, "\t"), {text: " value", caret: 7});+ assert.deepEqual(outdentAtCaret(" \tvalue", 8), {text: "\tvalue", caret: 6});+ assert.deepEqual(outdentAtCaret("value ", 9), {text: "value ", caret: 9});+ assert.deepEqual(outdentAtCaret(" value tail\t", 16), {text: "value tail\t", caret: 12});+});++test("collapsed code block outdent changes only the current line, including blank lines", () => {+ assert.deepEqual(outdentAtCaret(" first\n value\n last", 10),+ {text: " first\nvalue\n last", caret: 10});+ assert.deepEqual(outdentAtCaret(" first\n value\n last", 19),+ {text: " first\nvalue\n last", caret: 15});+ assert.deepEqual(outdentAtCaret("value\n \nnext", 7), {text: "value\n\nnext", caret: 6});+ assert.deepEqual(outdentAtCaret("value\n\n next", 6), {text: "value\n\n next", caret: 6});+ assert.deepEqual(outdentAtCaret("value\n", 6), {text: "value\n", caret: 6});+ assert.deepEqual(outdentAtCaret("", 0), {text: "", caret: 0});});diff --git a/app/src/protyle/wysiwyg/codeBlockUtil.ts b/app/src/protyle/wysiwyg/codeBlockUtil.tsindex d2ddc1d767..aa22a7e106 100644--- a/app/src/protyle/wysiwyg/codeBlockUtil.ts+++ b/app/src/protyle/wysiwyg/codeBlockUtil.ts@@ -37,21 +37,15 @@ export const updateCodeBlockLines = (text: string, tabSpace: string, outdent = freturn line.substring(spaceCount);}).join("\n");-export const getCodeBlockDeleteStart = (text: string, caret: number, tabSpace: string) => {- const rangeEnd = Math.min(Math.max(caret, 0), text.length);- const lineStart = rangeEnd === 0 ? 0 : text.lastIndexOf("\n", rangeEnd - 1) + 1;- if (rangeEnd <= lineStart) {- return rangeEnd;- }- if (text[rangeEnd - 1] === "\t") {- return rangeEnd - 1;- }- if (tabSpace === "\t") {- return rangeEnd;- }- let rangeStart = rangeEnd;- while (rangeStart > lineStart && rangeEnd - rangeStart < tabSpace.length && text[rangeStart - 1] === " ") {- rangeStart--;- }- return rangeStart;+export const getCodeBlockOutdentRange = (text: string, caret: number, tabSpace: string) => {+ const position = Math.min(Math.max(caret, 0), text.length);+ const lineStart = getCodeBlockLineRange(text, position, position).start;+ const lineEnd = text.indexOf("\n", lineStart);+ const line = text.substring(lineStart, lineEnd < 0 ? text.length : lineEnd);+ const removed = line.length - updateCodeBlockLines(line, tabSpace, true).length;+ return {+ start: lineStart,+ end: lineStart + removed,+ caret: Math.max(lineStart, position - removed),+ };};diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.tsindex 375035fe31..893a24f498 100644--- a/app/src/protyle/wysiwyg/keydown.ts+++ b/app/src/protyle/wysiwyg/keydown.ts@@ -2592,11 +2592,8 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {countBlockWord([], protyle);}if (tabNodeElement.getAttribute("data-type") === "NodeCodeBlock") {- if (selectText !== "" || !event.shiftKey) {- tabCodeBlock(protyle, tabNodeElement, tabRange, event.shiftKey);- return true;- }- return;+ tabCodeBlock(protyle, tabNodeElement, tabRange, event.shiftKey);+ return true;}if (!event.shiftKey) {document.execCommand("insertHTML", false, window.siyuan.config.editor.codeTabSpaces === 0 ? "\t" : "".padStart(window.siyuan.config.editor.codeTabSpaces, " "));