Skip to content

Commit b589459

Browse files
committed
🎨 #13881
1 parent 3b04d31 commit b589459

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

app/src/protyle/util/selection.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export const getRangeByPoint = (x: number, y: number) => {
135135
return range;
136136
};
137137

138-
export const getEditorRange = (element: Element) => {
138+
export const getEditorRange = (element: Element): Range => {
139139
let range: Range;
140140
if (getSelection().rangeCount > 0) {
141141
range = getSelection().getRangeAt(0);
@@ -153,7 +153,10 @@ export const getEditorRange = (element: Element) => {
153153
}
154154

155155
if (element.classList.contains("li") || element.classList.contains("list")) {
156-
return getEditorRange(element.querySelector("[data-node-id]"));
156+
const childElement = element.querySelector("[data-node-id]")
157+
if (childElement) {
158+
return getEditorRange(childElement);
159+
}
157160
}
158161

159162
// 代码块过长,在代码块的下一个块前删除,代码块会滚动到顶部,因粗需要 preventScroll
@@ -171,9 +174,9 @@ export const getEditorRange = (element: Element) => {
171174
if (!targetElement) {
172175
const type = element.getAttribute("data-type");
173176
if (type === "NodeThematicBreak") {
174-
targetElement= element.firstElementChild;
177+
targetElement = element.firstElementChild;
175178
} else if (type === "NodeBlockQueryEmbed") {
176-
targetElement = element.lastElementChild.previousElementSibling?.firstChild;
179+
targetElement = element.lastElementChild.previousElementSibling?.firstChild;
177180
} else if (["NodeMathBlock", "NodeHTMLBlock"].includes(type)) {
178181
targetElement = element.lastElementChild.previousElementSibling?.lastElementChild?.firstChild;
179182
} else if (type === "NodeVideo") {
@@ -575,8 +578,8 @@ export const focusBlock = (element: Element, parentElement?: HTMLElement, toStar
575578
genRenderFrame(element);
576579
range.setStart(element.firstElementChild.lastElementChild.firstChild, 0);
577580
setRange = true;
578-
} else if (type === "NodeHTMLBlock") {
579-
range.selectNodeContents(element.lastElementChild.previousElementSibling.lastElementChild.firstChild, 0);
581+
} else if (type === "NodeHTMLBlock") {
582+
range.setStart(element.lastElementChild.previousElementSibling.lastElementChild.firstChild, 0);
580583
range.collapse(true);
581584
setRange = true;
582585
} else if (type === "NodeIFrame" || type === "NodeWidget") {

app/src/protyle/wysiwyg/getBlock.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,18 @@ export const hasNextSibling = (element: Node) => {
164164
};
165165

166166
export const isEndOfBlock = (range: Range) => {
167-
if (range.endContainer.textContent.length !== range.endOffset) {
168-
return false;
167+
if (range.endContainer.nodeType === 3 &&
168+
range.endContainer.textContent.length !== range.endOffset &&
169+
range.endContainer.textContent !== Constants.ZWSP &&
170+
range.endContainer.textContent !== "\n") {
171+
return false;
169172
}
170173

171174
let nextSibling = range.endContainer;
175+
if (range.endContainer.nodeType !== 3 && range.endContainer.childNodes[range.endOffset]) {
176+
nextSibling = range.endContainer.childNodes[range.endOffset];
177+
}
178+
172179
while (nextSibling) {
173180
if (hasNextSibling(nextSibling)) {
174181
return false;

app/src/protyle/wysiwyg/keydown.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -853,11 +853,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
853853
}
854854
// 需使用 innerText,否则 br 无法传唤为 /n https://github.com/siyuan-note/siyuan/issues/12066
855855
// 段末反向删除 https://github.com/siyuan-note/insider/issues/274
856-
if (position.end === editElement.innerText.length ||
857-
// 软换行后删除 https://github.com/siyuan-note/siyuan/issues/11118
858-
(position.end === editElement.innerText.length - 1 && editElement.innerText.endsWith("\n")) ||
859-
// 图片后无内容删除 https://github.com/siyuan-note/siyuan/issues/11868
860-
(position.end === editElement.innerText.length - 1 && editElement.innerText.endsWith(Constants.ZWSP))) {
856+
if (isEndOfBlock(range)) {
861857
const nextElement = getNextBlock(getTopAloneElement(nodeElement));
862858
if (nextElement) {
863859
const nextRange = focusBlock(nextElement);
@@ -934,8 +930,8 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
934930
event.preventDefault();
935931
return;
936932
}
937-
// 图片后为空格,在空格后删除 https://github.com/siyuan-note/siyuan/issues/13949
938933
if (range.startOffset === 1 && range.startContainer.textContent.length === 1) {
934+
// 图片后为空格,在空格后删除 https://github.com/siyuan-note/siyuan/issues/13949
939935
const rangePreviousElement = hasPreviousSibling(range.startContainer) as HTMLElement;
940936
const rangeNextElement = hasNextSibling(range.startContainer) as HTMLElement;
941937
if (rangePreviousElement && rangePreviousElement.nodeType === 1 && rangePreviousElement.classList.contains("img") &&
@@ -949,6 +945,17 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
949945
event.preventDefault();
950946
return;
951947
}
948+
// 图片前有一个字符,在字符后删除
949+
if (position.start === 1 && !rangePreviousElement && rangeNextElement && rangeNextElement.nodeType === 1 && rangeNextElement.classList.contains("img")) {
950+
const wbrElement = document.createElement("wbr");
951+
range.insertNode(wbrElement);
952+
const oldHTML = nodeElement.outerHTML;
953+
wbrElement.previousSibling.textContent = Constants.ZWSP;
954+
updateTransaction(protyle, nodeElement.getAttribute("data-node-id"), nodeElement.outerHTML, oldHTML);
955+
focusByWbr(nodeElement, range);
956+
event.preventDefault();
957+
return;
958+
}
952959
}
953960
// 代码块中空行 ⌘+Del 异常 https://ld246.com/article/1663166544901
954961
if (nodeElement.classList.contains("code-block") && isOnlyMeta(event) &&

0 commit comments

Comments
 (0)