This commit is contained in:
Vanessa 2022-11-03 00:09:44 +08:00
parent d7f6456424
commit abc87f9500
4 changed files with 183 additions and 78 deletions

View file

@ -259,6 +259,8 @@ export class Files extends Model {
this.getLeaf(target, notebookId);
this.setCurrent(target, false);
}
this.element.querySelector('[select-end="true"]')?.removeAttribute("select-end");
this.element.querySelector('[select-start="true"]')?.removeAttribute("select-start");
window.siyuan.menus.menu.remove();
event.stopPropagation();
event.preventDefault();

View file

@ -168,7 +168,7 @@ export const downSelect = (options: {
options.event.preventDefault();
};
export const getStartEndElement = (selectElements: NodeListOf<Element>) => {
export const getStartEndElement = (selectElements: NodeListOf<Element> | Element[]) => {
let startElement;
let endElement;
selectElements.forEach(item => {

View file

@ -182,3 +182,49 @@ export const hasPreviousSibling = (element: Node) => {
}
return false;
};
export const getNextFileLi = (current: Element) => {
let nextElement = current.nextElementSibling
if (nextElement) {
if (nextElement.tagName === "LI") {
return nextElement
} else if (nextElement.tagName === "UL") {
return nextElement.firstElementChild
}
return false;
}
nextElement = current.parentElement
while (nextElement.tagName === "UL") {
if (!nextElement.nextElementSibling) {
nextElement = nextElement.parentElement
} else if (nextElement.nextElementSibling.tagName === "LI") {
return nextElement.nextElementSibling
} else if (nextElement.nextElementSibling.tagName === "UL") {
return nextElement.nextElementSibling.firstElementChild;
}
}
return false;
}
export const getPreviousFileLi = (current: Element) => {
let previousElement = current.previousElementSibling
if (previousElement) {
if (previousElement.tagName === "LI") {
return previousElement
} else if (previousElement.tagName === "UL") {
return previousElement.lastElementChild
}
return false;
}
previousElement = current.parentElement
while (previousElement.tagName === "UL") {
if (!previousElement.previousElementSibling) {
previousElement = previousElement.parentElement
} else if (previousElement.previousElementSibling.tagName === "LI") {
return previousElement.previousElementSibling;
} else if (previousElement.previousElementSibling.tagName === "UL") {
return previousElement.previousElementSibling.lastElementChild;
}
}
return false;
}

View file

@ -39,6 +39,8 @@ import {deleteFile} from "../editor/deleteFile";
import {escapeHtml} from "./escape";
import {syncGuide} from "../sync/syncGuide";
import {showPopover} from "../block/popover";
import {getStartEndElement} from "../protyle/wysiwyg/commonHotkey";
import {getNextFileLi, getPreviousFileLi} from "../protyle/wysiwyg/getBlock";
const getRightBlock = (element: HTMLElement, x: number, y: number) => {
let index = 1;
@ -953,6 +955,57 @@ const fileTreeKeydown = (event: KeyboardEvent) => {
event.preventDefault();
return true;
}
if (event.shiftKey) {
if (event.key === "ArrowUp") {
const startEndElement = getStartEndElement(liElements);
if (startEndElement.startElement.getBoundingClientRect().top >= startEndElement.endElement.getBoundingClientRect().top) {
const previousElement = getPreviousFileLi(startEndElement.endElement)
if (previousElement) {
previousElement.classList.add("b3-list-item--focus");
previousElement.setAttribute("select-end", "true");
startEndElement.endElement.removeAttribute("select-end");
const previousRect = previousElement.getBoundingClientRect();
const fileRect = files.element.getBoundingClientRect();
if (previousRect.top < fileRect.top || previousRect.bottom > fileRect.bottom) {
previousElement.scrollIntoView(previousRect.top < fileRect.top);
}
}
} else {
startEndElement.endElement.classList.remove("b3-list-item--focus");
startEndElement.endElement.removeAttribute("select-end");
const previousElement = getPreviousFileLi(startEndElement.endElement);
if (previousElement) {
previousElement.setAttribute("select-end", "true");
}
}
} else if (event.key === "ArrowDown") {
const startEndElement = getStartEndElement(liElements);
if (startEndElement.startElement.getBoundingClientRect().top <= startEndElement.endElement.getBoundingClientRect().top) {
const nextElement = getNextFileLi(startEndElement.endElement);
if (nextElement) {
nextElement.classList.add("b3-list-item--focus");
nextElement.setAttribute("select-end", "true");
startEndElement.endElement.removeAttribute("select-end");
const nextRect = nextElement.getBoundingClientRect();
const fileRect = files.element.getBoundingClientRect();
if (nextRect.top < fileRect.top || nextRect.bottom > fileRect.bottom) {
nextElement.scrollIntoView(nextRect.top < fileRect.top);
}
}
} else {
startEndElement.endElement.classList.remove("b3-list-item--focus");
startEndElement.endElement.removeAttribute("select-end");
const nextElement = getNextFileLi(startEndElement.endElement);
if (nextElement) {
nextElement.setAttribute("select-end", "true");
}
}
}
return;
} else {
files.element.querySelector('[select-end="true"]')?.removeAttribute("select-end");
files.element.querySelector('[select-start="true"]')?.removeAttribute("select-start");
if ((event.key === "ArrowRight" && !liElements[0].querySelector(".b3-list-item__arrow--open") && !liElements[0].querySelector(".b3-list-item__toggle").classList.contains("fn__hidden")) ||
(event.key === "ArrowLeft" && liElements[0].querySelector(".b3-list-item__arrow--open"))) {
files.getLeaf(liElements[0], notebookId);
@ -964,18 +1017,18 @@ const fileTreeKeydown = (event: KeyboardEvent) => {
event.preventDefault();
return true;
}
const fileRect = files.element.getBoundingClientRect();
if (event.key === "ArrowLeft") {
let parentElement = liElements[0].parentElement.previousElementSibling;
if (parentElement) {
if (parentElement.tagName !== "LI") {
parentElement = files.element.querySelector(".b3-list-item");
}
liElements.forEach((item, index) => {
liElements.forEach((item) => {
item.classList.remove("b3-list-item--focus")
})
parentElement.classList.add("b3-list-item--focus");
const parentRect = parentElement.getBoundingClientRect();
const fileRect = files.element.getBoundingClientRect();
if (parentRect.top < fileRect.top || parentRect.bottom > fileRect.bottom) {
parentElement.scrollIntoView(parentRect.top < fileRect.top);
}
@ -1002,11 +1055,12 @@ const fileTreeKeydown = (event: KeyboardEvent) => {
}
}
if (nextElement.classList.contains("b3-list-item")) {
liElements.forEach((item, index) => {
liElements.forEach((item) => {
item.classList.remove("b3-list-item--focus")
})
nextElement.classList.add("b3-list-item--focus");
const nextRect = nextElement.getBoundingClientRect();
const fileRect = files.element.getBoundingClientRect();
if (nextRect.top < fileRect.top || nextRect.bottom > fileRect.bottom) {
nextElement.scrollIntoView(nextRect.top < fileRect.top);
}
@ -1034,11 +1088,12 @@ const fileTreeKeydown = (event: KeyboardEvent) => {
}
}
if (previousElement.classList.contains("b3-list-item")) {
liElements.forEach((item, index) => {
liElements.forEach((item) => {
item.classList.remove("b3-list-item--focus")
})
previousElement.classList.add("b3-list-item--focus");
const previousRect = previousElement.getBoundingClientRect();
const fileRect = files.element.getBoundingClientRect();
if (previousRect.top < fileRect.top || previousRect.bottom > fileRect.bottom) {
previousElement.scrollIntoView(previousRect.top < fileRect.top);
}
@ -1046,6 +1101,8 @@ const fileTreeKeydown = (event: KeyboardEvent) => {
event.preventDefault();
return true;
}
}
if (event.key === "Delete" || (event.key === "Backspace" && isMac())) {
window.siyuan.menus.menu.remove();
liElements.forEach(item => {