Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve Shift+Click selection on editor whitespace https://github.com/siyuan-note/siyuan/issues/11960

Danielaf7213533c7a
diff --git a/app/src/protyle/ui/initUI.ts b/app/src/protyle/ui/initUI.ts
index a80e531210..55dab3318f 100644
--- a/app/src/protyle/ui/initUI.ts
+++ b/app/src/protyle/ui/initUI.ts
@@ -152,6 +152,25 @@ export const initUI = (protyle: IProtyle) => {
});
}, Constants.TIMEOUT_LOAD);
}, {passive: true});
+ protyle.contentElement.addEventListener("mousedown", (event: MouseEvent & { target: HTMLElement }) => {
+ if (event.button !== 0 || !event.shiftKey || event.target !== protyle.contentElement) {
+ return;
+ }
+ const eventProtyleElement = hasClosestByClassName(event.target, "protyle", true);
+ if (eventProtyleElement && eventProtyleElement !== protyle.element) {
+ return;
+ }
+ const lastElement = protyle.wysiwyg.element.lastElementChild;
+ if (!lastElement || event.clientY <= lastElement.getBoundingClientRect().bottom) {
+ return;
+ }
+ // 文档末尾空白位于 wysiwyg 外层,需在 click 聚焦末尾块之前完成 Shift 范围选择
+ // https://github.com/siyuan-note/siyuan/issues/11960
+ if (protyle.wysiwyg.selectByShiftClick(protyle, event, undefined, true)) {
+ event.preventDefault();
+ event.stopPropagation();
+ }
+ });
protyle.contentElement.addEventListener("click", (event: MouseEvent & { target: HTMLElement }) => {
const eventProtyleElement = hasClosestByClassName(event.target, "protyle", true);
if (eventProtyleElement && eventProtyleElement !== protyle.element) {
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 0c2bd6a52a..b01f5edb7d 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -312,6 +312,66 @@ export class WYSIWYG {
}
}
+ public selectByShiftClick(protyle: IProtyle, event: MouseEvent, targetBlockElement?: HTMLElement,
+ resolveTargetByPoint = false): boolean {
+ const selection = getSelection();
+ let startElement: HTMLElement | undefined;
+ let endElement = targetBlockElement;
+ let shiftClickBlockPoint: IShiftClickBlockPoint | undefined;
+ // Electron 更新后 shift 向上点击获取的 range 不为上一个位置的 https://github.com/siyuan-note/siyuan/issues/9334
+ if (selection.rangeCount > 0) {
+ startElement = hasClosestBlock(selection.getRangeAt(0).startContainer) as HTMLElement;
+ }
+ if (startElement && (resolveTargetByPoint || !endElement)) {
+ shiftClickBlockPoint = getShiftClickBlockByPoint(this.element, startElement,
+ event.clientX, event.clientY);
+ endElement = shiftClickBlockPoint?.blockElement;
+ }
+ if (startElement && endElement && startElement !== endElement) {
+ const blockRange = getBlockRangeSelectElements(startElement, endElement);
+ startElement = blockRange.startElement;
+ endElement = blockRange.endElement;
+ const selectElements = blockRange.selectElements;
+ const toDown = blockRange.toDown;
+ if (selectElements.length === 1 && !selectElements[0].classList.contains("list") &&
+ !selectElements[0].classList.contains("bq") && !selectElements[0].classList.contains("callout") &&
+ !selectElements[0].classList.contains("sb")) {
+ // 单个 p 不选中
+ } else {
+ const ids: string[] = [];
+ const hasSelectClassElement = this.element.querySelector(".protyle-wysiwyg--select");
+ if (!hasSelectClassElement && protyle.scroll && !protyle.scroll.element.classList.contains("fn__none") &&
+ !protyle.scroll.keepLazyLoad &&
+ (startElement.getBoundingClientRect().top < -protyle.contentElement.clientHeight * 2 ||
+ endElement.getBoundingClientRect().bottom > protyle.contentElement.clientHeight * 2)) {
+ showMessage(window.siyuan.languages.crossKeepLazyLoad);
+ }
+ selectElements.forEach(item => {
+ if (!hasClosestByClassName(item, "protyle-wysiwyg--select")) {
+ item.classList.add("protyle-wysiwyg--select");
+ ids.push(item.getAttribute("data-node-id"));
+ // 清除选中的子块 https://ld246.com/article/1667826582251
+ item.querySelectorAll(".protyle-wysiwyg--select").forEach(subItem => {
+ subItem.classList.remove("protyle-wysiwyg--select");
+ });
+ }
+ });
+ countBlockWord(ids);
+ if (toDown) {
+ focusBlock(selectElements[selectElements.length - 1], protyle.wysiwyg.element, false);
+ } else {
+ focusBlock(selectElements[0], protyle.wysiwyg.element, false);
+ }
+ }
+ return true;
+ }
+ if (!this.element.querySelector(".protyle-wysiwyg--select") && shiftClickBlockPoint &&
+ startElement === endElement) {
+ return extendSelectionToBlockSide(selection, endElement, shiftClickBlockPoint.toStart);
+ }
+ return false;
+ }
+
constructor(protyle: IProtyle) {
this.element = document.createElement("div");
this.element.className = "protyle-wysiwyg";
@@ -980,58 +1040,12 @@ export class WYSIWYG {
event.stopPropagation();
return;
}
- let startElement;
- let endElement = nodeElement;
- let shiftClickBlockPoint: IShiftClickBlockPoint | undefined;
- // Electron 更新后 shift 向上点击获取的 range 不为上一个位置的 https://github.com/siyuan-note/siyuan/issues/9334
- if (getSelection().rangeCount > 0) {
- startElement = hasClosestBlock(getSelection().getRangeAt(0).startContainer) as HTMLElement;
- }
// 块间空白和文档末尾没有直接对应的块,需沿锚点方向解析终点
// https://github.com/siyuan-note/siyuan/issues/11960
- if (startElement && (!endElement || (target === endElement && isContainerBlock(endElement)))) {
- shiftClickBlockPoint = getShiftClickBlockByPoint(this.element, startElement,
- event.clientX, event.clientY);
- endElement = shiftClickBlockPoint?.blockElement;
- }
- // shift 多选
- if (startElement && endElement && startElement !== endElement) {
- const blockRange = getBlockRangeSelectElements(startElement, endElement);
- startElement = blockRange.startElement;
- endElement = blockRange.endElement;
- const selectElements = blockRange.selectElements;
- const toDown = blockRange.toDown;
- if (selectElements.length === 1 && !selectElements[0].classList.contains("list") &&
- !selectElements[0].classList.contains("bq") && !selectElements[0].classList.contains("callout") &&
- !selectElements[0].classList.contains("sb")) {
- // 单个 p 不选中
- } else {
- const ids: string[] = [];
- if (!hasSelectClassElement && protyle.scroll && !protyle.scroll.element.classList.contains("fn__none") && !protyle.scroll.keepLazyLoad &&
- (startElement.getBoundingClientRect().top < -protyle.contentElement.clientHeight * 2 || endElement.getBoundingClientRect().bottom > protyle.contentElement.clientHeight * 2)) {
- showMessage(window.siyuan.languages.crossKeepLazyLoad);
- }
- selectElements.forEach(item => {
- if (!hasClosestByClassName(item, "protyle-wysiwyg--select")) {
- item.classList.add("protyle-wysiwyg--select");
- ids.push(item.getAttribute("data-node-id"));
- // 清除选中的子块 https://ld246.com/article/1667826582251
- item.querySelectorAll(".protyle-wysiwyg--select").forEach(subItem => {
- subItem.classList.remove("protyle-wysiwyg--select");
- });
- }
- });
- countBlockWord(ids);
- if (toDown) {
- focusBlock(selectElements[selectElements.length - 1], protyle.wysiwyg.element, false);
- } else {
- focusBlock(selectElements[0], protyle.wysiwyg.element, false);
- }
- }
- event.preventDefault();
- } else if (!hasSelectClassElement && shiftClickBlockPoint && startElement === endElement &&
- extendSelectionToBlockSide(getSelection(), endElement, shiftClickBlockPoint.toStart)) {
+ if (this.selectByShiftClick(protyle, event, nodeElement, !nodeElement || target === nodeElement)) {
+ this.preventClick = true;
event.preventDefault();
+ event.stopPropagation();
}
return;
}