Skip to content

Commit

Permalink
♻️ #9846
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Dec 9, 2023
1 parent 1dc5a37 commit 5fac682
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 111 deletions.
20 changes: 9 additions & 11 deletions app/src/boot/globalEvent/searchKeydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from "path";
/// #endif
import {matchHotKey} from "../../protyle/util/hotKey";
import {fetchPost} from "../../util/fetch";
import {openFileById} from "../../editor/util";
import {checkFold, openFileById} from "../../editor/util";
import {Constants} from "../../constants";
import {newFileByName} from "../../util/newFile";
import {App} from "../../index";
Expand Down Expand Up @@ -90,19 +90,18 @@ export const searchKeydown = (app: App, event: KeyboardEvent) => {
if (!isAsset) {
if (matchHotKey(window.siyuan.config.keymap.editor.general.insertRight.custom, event)) {
const id = currentList.getAttribute("data-node-id");
fetchPost("/api/block/checkBlockFold", {id}, (foldResponse) => {
checkFold(id, (zoomIn, action) => {
openFileById({
app,
id,
position: "right",
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] :
(id === currentList.getAttribute("data-root-id") ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ROOTSCROLL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT]),
zoomIn: foldResponse.data
action,
zoomIn
});
if (dialog) {
dialog.destroy({focus: "false"});
}
});
})
return true;
}
const id = currentList.getAttribute("data-node-id");
Expand Down Expand Up @@ -197,18 +196,17 @@ export const searchKeydown = (app: App, event: KeyboardEvent) => {
replace(element, config, edit, false);
} else {
const id = currentList.getAttribute("data-node-id");
fetchPost("/api/block/checkBlockFold", {id}, (foldResponse) => {
checkFold(id, (zoomIn, action) => {
openFileById({
app,
id,
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] :
(id === currentList.getAttribute("data-root-id") ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ROOTSCROLL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT]),
zoomIn: foldResponse.data
action,
zoomIn
});
if (dialog) {
dialog.destroy({focus: "false"});
}
});
})
}
} else {
/// #if !BROWSER
Expand Down
4 changes: 2 additions & 2 deletions app/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ export abstract class Constants {
public static readonly CB_GET_ALL = "cb-get-all"; // 获取所有块
public static readonly CB_GET_BACKLINK = "cb-get-backlink"; // 悬浮窗为传递型需展示上下文
public static readonly CB_GET_UNUNDO = "cb-get-unundo"; // 不需要记录历史
public static readonly CB_GET_SCROLL = "cb-get-scroll"; // 滚动到指定位置
public static readonly CB_GET_SCROLL = "cb-get-scroll"; // 滚动到指定位置,用于直接打开文档,必有 rootID
public static readonly CB_GET_CONTEXT = "cb-get-context"; // 包含上下文
public static readonly CB_GET_ROOTSCROLL = "cb-get-rootscroll"; // 如果为 rootID 就滚动到指定位置
public static readonly CB_GET_ROOTSCROLL = "cb-get-rootscroll"; // 如果为 rootID 就滚动到指定位置,必有 rootID
public static readonly CB_GET_HTML = "cb-get-html"; // 直接渲染,不需要再 /api/block/getDocInfo,否则搜索表格无法定位
public static readonly CB_GET_HISTORY = "cb-get-history"; // 历史渲染

Expand Down
4 changes: 2 additions & 2 deletions app/src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Editor extends Model {
app: App,
tab: Tab,
blockId: string,
rootId?: string, // 使用 rootId 会优先使用本地 filepositon 定位
rootId: string,
mode?: TEditorMode,
action?: string[],
}) {
Expand All @@ -38,7 +38,7 @@ export class Editor extends Model {
private initProtyle(options: {
blockId: string,
action?: string[]
rootId?: string,
rootId: string,
mode?: TEditorMode,
}) {
this.editor = new Protyle(this.app, this.element, {
Expand Down
13 changes: 12 additions & 1 deletion app/src/editor/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ import {Search} from "../search";
import {App} from "../index";
import {newCardModel} from "../card/newCardTab";

export const checkFold = (id: string, cb: (zoomIn: boolean, action: string[]) => void) => {
if (!id) {
return;
}
fetchPost("/api/block/checkBlockFold", {id}, (foldResponse) => {
cb(foldResponse.data, foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL])
});
}

export const openFileById = async (options: {
app: App,
id: string,
Expand Down Expand Up @@ -472,13 +481,15 @@ const newTab = (options: IOpenFileOptions) => {
app: options.app,
tab,
blockId: options.id,
rootId: options.rootID,
action: [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS],
});
} else {
editor = new Editor({
app: options.app,
tab,
blockId: options.id,
rootId: options.rootID,
mode: options.mode,
action: options.action,
});
Expand Down Expand Up @@ -538,7 +549,7 @@ export const updatePanelByEditor = (options: {
updateOutline(models, options.protyle, options.reload);
updateBacklinkGraph(models, options.protyle);
options.protyle.app.plugins.forEach(item => {
item.eventBus.emit("switch-protyle", {protyle:options.protyle});
item.eventBus.emit("switch-protyle", {protyle: options.protyle});
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion app/src/layout/Wnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ export class Wnd {
openFileById({
app: this.app,
id: keepCursorId,
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_SCROLL]
});
}
currentTab.headElement.removeAttribute("keep-cursor");
Expand Down
75 changes: 42 additions & 33 deletions app/src/layout/dock/Bookmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {setPanelFocus} from "../util";
import {getDockByType} from "../tabUtil";
import {fetchPost} from "../../util/fetch";
import {updateHotkeyTip} from "../../protyle/util/compatibility";
import {openFileById} from "../../editor/util";
import {Constants} from "../../constants";
import {checkFold, openFileById} from "../../editor/util";
import {hasClosestByClassName} from "../../protyle/util/hasClosest";
import {openBookmarkMenu} from "../../menus/bookmark";
import {App} from "../../index";
Expand Down Expand Up @@ -84,44 +83,54 @@ export class Bookmark extends Model {
return;
}
}
const id = element.getAttribute("data-node-id");
if (id) {
fetchPost("/api/block/checkBlockFold", {id}, (foldResponse) => {
openFileById({
app,
id,
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL],
zoomIn: foldResponse.data
});
const id = element.getAttribute("data-node-id")
checkFold(id, (zoomIn, action: string[]) => {
openFileById({
app,
id,
action,
zoomIn
});
}
});
},
rightClick: (element: HTMLElement, event: MouseEvent) => {
openBookmarkMenu(element, event, this);
},
ctrlClick(element: HTMLElement) {
openFileById({
app,
id: element.getAttribute("data-node-id"),
keepCursor: true,
action: [Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]
});
ctrlClick: (element: HTMLElement) => {
const id = element.getAttribute("data-node-id")
checkFold(id, (zoomIn, action: string[]) => {
openFileById({
app,
id,
keepCursor: true,
action,
zoomIn
});
})
},
altClick(element: HTMLElement) {
openFileById({
app,
id: element.getAttribute("data-node-id"),
position: "right",
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]
});
altClick: (element: HTMLElement,) => {
const id = element.getAttribute("data-node-id")
checkFold(id, (zoomIn, action: string[]) => {
openFileById({
app,
id,
position: "bottom",
action,
zoomIn
});
})
},
shiftClick(element: HTMLElement) {
openFileById({
app,
id: element.getAttribute("data-node-id"),
position: "bottom",
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]
});
shiftClick: (element: HTMLElement) => {
const id = element.getAttribute("data-node-id")
checkFold(id, (zoomIn, action: string[]) => {
openFileById({
app,
id,
position: "bottom",
action,
zoomIn
});
})
},
blockExtHTML: '<span class="b3-list-item__action"><svg><use xlink:href="#iconMore"></use></svg></span>',
topExtHTML: '<span class="b3-list-item__action"><svg><use xlink:href="#iconMore"></use></svg></span>',
Expand Down
45 changes: 27 additions & 18 deletions app/src/layout/dock/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {addScript} from "../../protyle/util/addScript";
import {BlockPanel} from "../../block/Panel";
import {fullscreen} from "../../protyle/breadcrumb/action";
import {fetchPost} from "../../util/fetch";
import {isCurrentEditor, openFileById} from "../../editor/util";
import {checkFold, isCurrentEditor, openFileById} from "../../editor/util";
import {updateHotkeyTip} from "../../protyle/util/compatibility";
import {openGlobalSearch} from "../../search/util";
import {App} from "../../index";
Expand Down Expand Up @@ -652,19 +652,25 @@ export class Graph extends Model {
return;
}
if (window.siyuan.shiftIsPressed) {
openFileById({
app: this.app,
id: node.id,
position: "bottom",
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]
});
checkFold(node.id, (zoomIn, action: string[]) => {
openFileById({
app: this.app,
id: node.id,
position: "bottom",
action,
zoomIn
});
})
} else if (window.siyuan.altIsPressed) {
openFileById({
app: this.app,
id: node.id,
position: "right",
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]
});
checkFold(node.id, (zoomIn, action: string[]) => {
openFileById({
app: this.app,
id: node.id,
position: "right",
action,
zoomIn
});
})
} else if (window.siyuan.ctrlIsPressed) {
window.siyuan.blockPanels.push(new BlockPanel({
app: this.app,
Expand All @@ -674,11 +680,14 @@ export class Graph extends Model {
nodeIds: [node.id],
}));
} else {
openFileById({
app: this.app,
id: node.id,
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]
});
checkFold(node.id, (zoomIn, action: string[]) => {
openFileById({
app: this.app,
id: node.id,
action,
zoomIn
});
})
}
});
}, 1000);
Expand Down
3 changes: 2 additions & 1 deletion app/src/layout/tabUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ export const copyTab = (app: App, tab: Tab) => {
model = new Editor({
app,
tab: newTab,
blockId: tab.model.editor.protyle.block.id
blockId: tab.model.editor.protyle.block.id,
rootId: tab.model.editor.protyle.block.rootID
});
} else if (tab.model instanceof Asset) {
model = new Asset({
Expand Down
1 change: 1 addition & 0 deletions app/src/mobile/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const openMobileFileById = (app: App, id: string, action = [Constants.CB_
} else {
window.siyuan.mobile.editor = new Protyle(app, document.getElementById("editor"), {
blockId: id,
rootId: data.data.rootID,
action,
render: {
scroll: true,
Expand Down
5 changes: 4 additions & 1 deletion app/src/mobile/util/initFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {syncGuide} from "../../sync/syncGuide";
import {Inbox} from "../../layout/dock/Inbox";
import {App} from "../../index";
import {setTitle} from "../../dialog/processSystem";
import {checkFold} from "../../editor/util";

export const initFramework = (app: App, isStart: boolean) => {
setInlineStyle();
Expand Down Expand Up @@ -134,7 +135,9 @@ export const initFramework = (app: App, isStart: boolean) => {
} else {
fetchPost("/api/block/getRecentUpdatedBlocks", {}, (response) => {
if (response.data.length !== 0) {
openMobileFileById(app, response.data[0].id, [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);
checkFold(response.data[0].id, (zoomIn) => {
openMobileFileById(app, response.data[0].id, zoomIn ? [Constants.CB_GET_ALL, Constants.CB_GET_HL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);
});
} else {
setEmpty(app);
}
Expand Down
38 changes: 8 additions & 30 deletions app/src/protyle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,44 +228,22 @@ export class Protyle {
removeLoading(this.protyle);
return;
}
const filePosition = window.siyuan.storage[Constants.LOCAL_FILEPOSITION][options.blockId] ||
window.siyuan.storage[Constants.LOCAL_FILEPOSITION][options.rootId];

if (this.protyle.options.mode !== "preview" &&
!mergedOptions.action.includes(Constants.CB_GET_ALL) &&
(mergedOptions.action.includes(Constants.CB_GET_SCROLL) || mergedOptions.action.includes(Constants.CB_GET_ROOTSCROLL)) &&
filePosition) {
options.rootId && window.siyuan.storage[Constants.LOCAL_FILEPOSITION][options.rootId] &&
(
mergedOptions.action.includes(Constants.CB_GET_SCROLL) ||
(mergedOptions.action.includes(Constants.CB_GET_ROOTSCROLL) && options.rootId === options.blockId)
)
) {
getDocByScroll({
protyle: this.protyle,
scrollAttr: filePosition,
scrollAttr: window.siyuan.storage[Constants.LOCAL_FILEPOSITION][options.rootId],
mergedOptions,
cb: () => {
this.afterOnGet(mergedOptions);
}
});
} else if (this.protyle.options.mode !== "preview" &&
(mergedOptions.action.includes(Constants.CB_GET_SCROLL) || mergedOptions.action.includes(Constants.CB_GET_ROOTSCROLL))) {
fetchPost("/api/block/getDocInfo", {
id: options.blockId
}, (response) => {
if (!mergedOptions.action.includes(Constants.CB_GET_SCROLL) &&
response.data.rootID !== options.blockId && mergedOptions.action.includes(Constants.CB_GET_ROOTSCROLL)) {
// 打开根文档保持上一次历史,否则按照原有 action 执行 https://github.com/siyuan-note/siyuan/issues/9082
this.getDoc(mergedOptions);
return;
}
if (window.siyuan.storage[Constants.LOCAL_FILEPOSITION][response.data.rootID]) {
getDocByScroll({
protyle: this.protyle,
scrollAttr: window.siyuan.storage[Constants.LOCAL_FILEPOSITION][response.data.rootID],
mergedOptions,
cb: () => {
this.afterOnGet(mergedOptions);
}
});
} else {
this.getDoc(mergedOptions);
}
});
} else {
this.getDoc(mergedOptions);
}
Expand Down
Loading

0 comments on commit 5fac682

Please sign in to comment.