Vanessa 2023-12-09 12:09:46 +08:00
parent e14ca5799b
commit 73505073e6
12 changed files with 42 additions and 59 deletions

View file

@ -21,7 +21,7 @@ import {setEmpty} from "../mobile/util/setEmpty";
import {hideAllElements, hideElements} from "../protyle/ui/hideElements"; import {hideAllElements, hideElements} from "../protyle/ui/hideElements";
import {App} from "../index"; import {App} from "../index";
import {saveScroll} from "../protyle/scroll/saveScroll"; import {saveScroll} from "../protyle/scroll/saveScroll";
import {isInAndroid, isInIOS} from "../protyle/util/compatibility"; import {isInAndroid, isInIOS, setStorageVal} from "../protyle/util/compatibility";
import {Plugin} from "../plugin"; import {Plugin} from "../plugin";
const updateTitle = (rootID: string, tab: Tab) => { const updateTitle = (rootID: string, tab: Tab) => {
@ -65,12 +65,14 @@ export const reloadSync = (app: App, data: { upsertRootIDs: string[], removeRoot
reloadProtyle(item.editor.protyle, false); reloadProtyle(item.editor.protyle, false);
updateTitle(item.editor.protyle.block.rootID, item.parent); updateTitle(item.editor.protyle.block.rootID, item.parent);
} else if (data.removeRootIDs.includes(item.editor.protyle.block.rootID)) { } else if (data.removeRootIDs.includes(item.editor.protyle.block.rootID)) {
item.parent.parent.removeTab(item.parent.id, false, false, false); item.parent.parent.removeTab(item.parent.id, false, false);
delete window.siyuan.storage[Constants.LOCAL_FILEPOSITION][item.editor.protyle.block.rootID];
setStorageVal(Constants.LOCAL_FILEPOSITION, window.siyuan.storage[Constants.LOCAL_FILEPOSITION]);
} }
}); });
allModels.graph.forEach(item => { allModels.graph.forEach(item => {
if (item.type === "local" && data.removeRootIDs.includes(item.rootId)) { if (item.type === "local" && data.removeRootIDs.includes(item.rootId)) {
item.parent.parent.removeTab(item.parent.id, false, false, false); item.parent.parent.removeTab(item.parent.id, false, false);
} else if (item.type !== "local" || data.upsertRootIDs.includes(item.rootId)) { } else if (item.type !== "local" || data.upsertRootIDs.includes(item.rootId)) {
item.searchGraph(false); item.searchGraph(false);
if (item.type === "local") { if (item.type === "local") {
@ -80,7 +82,7 @@ export const reloadSync = (app: App, data: { upsertRootIDs: string[], removeRoot
}); });
allModels.outline.forEach(item => { allModels.outline.forEach(item => {
if (item.type === "local" && data.removeRootIDs.includes(item.blockId)) { if (item.type === "local" && data.removeRootIDs.includes(item.blockId)) {
item.parent.parent.removeTab(item.parent.id, false, false, false); item.parent.parent.removeTab(item.parent.id, false, false);
} else if (item.type !== "local" || data.upsertRootIDs.includes(item.blockId)) { } else if (item.type !== "local" || data.upsertRootIDs.includes(item.blockId)) {
fetchPost("/api/outline/getDocOutline", { fetchPost("/api/outline/getDocOutline", {
id: item.blockId, id: item.blockId,
@ -94,7 +96,7 @@ export const reloadSync = (app: App, data: { upsertRootIDs: string[], removeRoot
}); });
allModels.backlink.forEach(item => { allModels.backlink.forEach(item => {
if (item.type === "local" && data.removeRootIDs.includes(item.rootId)) { if (item.type === "local" && data.removeRootIDs.includes(item.rootId)) {
item.parent.parent.removeTab(item.parent.id, false, false, false); item.parent.parent.removeTab(item.parent.id, false, false);
} else { } else {
item.refresh(); item.refresh();
if (item.type === "local") { if (item.type === "local") {

View file

@ -19,9 +19,9 @@ export class Editor extends Model {
app: App, app: App,
tab: Tab, tab: Tab,
blockId: string, blockId: string,
rootId?: string, // 使用 rootId 会优先使用本地 filepositon 定位
mode?: TEditorMode, mode?: TEditorMode,
action?: string[], action?: string[],
scrollAttr?: IScrollAttr
}) { }) {
super({ super({
app: options.app, app: options.app,
@ -38,19 +38,19 @@ export class Editor extends Model {
private initProtyle(options: { private initProtyle(options: {
blockId: string, blockId: string,
action?: string[] action?: string[]
rootId?: string,
mode?: TEditorMode, mode?: TEditorMode,
scrollAttr?: IScrollAttr
}) { }) {
this.editor = new Protyle(this.app, this.element, { this.editor = new Protyle(this.app, this.element, {
action: options.action || [], action: options.action || [],
blockId: options.blockId, blockId: options.blockId,
rootId: options.rootId,
mode: options.mode, mode: options.mode,
render: { render: {
title: true, title: true,
background: true, background: true,
scroll: true, scroll: true,
}, },
scrollAttr: options.scrollAttr,
typewriterMode: true, typewriterMode: true,
after: (editor) => { after: (editor) => {
if (window.siyuan.editorIsFullscreen) { if (window.siyuan.editorIsFullscreen) {

View file

@ -277,7 +277,7 @@ export const openFile = async (options: IOpenFileOptions) => {
createdTab = newTab(options); createdTab = newTab(options);
wnd.addTab(createdTab); wnd.addTab(createdTab);
if (unUpdateTab && options.removeCurrentTab) { if (unUpdateTab && options.removeCurrentTab) {
wnd.removeTab(unUpdateTab.id, false, true, false); wnd.removeTab(unUpdateTab.id, false, false);
} }
} else { } else {
createdTab = newTab(options); createdTab = newTab(options);
@ -306,7 +306,6 @@ const getUnInitTab = (options: IOpenFileOptions) => {
} else { } else {
initObj.action = options.action; initObj.action = options.action;
} }
delete initObj.scrollAttr;
item.headElement.setAttribute("data-initdata", JSON.stringify(initObj)); item.headElement.setAttribute("data-initdata", JSON.stringify(initObj));
item.parent.switchTab(item.headElement); item.parent.switchTab(item.headElement);
return true; return true;

View file

@ -682,7 +682,7 @@ export class Wnd {
model.send("closews", {}); model.send("closews", {});
} }
private removeTabAction = (id: string, closeAll = false, hasSaveScroll = true, animate = true) => { private removeTabAction = (id: string, closeAll = false, animate = true) => {
clearCounter(); clearCounter();
this.children.find((item, index) => { this.children.find((item, index) => {
if (item.id === id) { if (item.id === id) {
@ -691,7 +691,7 @@ export class Wnd {
item.model.beforeDestroy(); item.model.beforeDestroy();
} }
} }
if (item.model instanceof Editor && hasSaveScroll) { if (item.model instanceof Editor) {
saveScroll(item.model.editor.protyle); saveScroll(item.model.editor.protyle);
} }
if (this.children.length === 1) { if (this.children.length === 1) {
@ -787,7 +787,7 @@ export class Wnd {
/// #endif /// #endif
}; };
public removeTab(id: string, closeAll = false, needSaveScroll = true, animate = true) { public removeTab(id: string, closeAll = false, animate = true) {
for (let index = 0; index < this.children.length; index++) { for (let index = 0; index < this.children.length; index++) {
const item = this.children[index]; const item = this.children[index];
if (item.id === id) { if (item.id === id) {
@ -796,9 +796,9 @@ export class Wnd {
showMessage(window.siyuan.languages.uploading); showMessage(window.siyuan.languages.uploading);
return; return;
} }
this.removeTabAction(id, closeAll, needSaveScroll, animate); this.removeTabAction(id, closeAll, animate);
} else { } else {
this.removeTabAction(id, closeAll, needSaveScroll, animate); this.removeTabAction(id, closeAll, animate);
} }
return; return;
} }

View file

@ -304,10 +304,6 @@ export const copyTab = (app: App, tab: Tab) => {
} else if (!tab.model && tab.headElement) { } else if (!tab.model && tab.headElement) {
const initData = JSON.parse(tab.headElement.getAttribute("data-initdata") || "{}"); const initData = JSON.parse(tab.headElement.getAttribute("data-initdata") || "{}");
if (initData) { if (initData) {
// 历史数据兼容 2023-05-24
if (initData.scrollAttr) {
initData.scrollAttr.rootId = initData.rootId;
}
model = newModelByInitData(app, newTab, initData); model = newModelByInitData(app, newTab, initData);
} }
} }
@ -320,7 +316,7 @@ export const closeTabByType = async (tab: Tab, type: "closeOthers" | "closeAll"
if (type === "closeOthers") { if (type === "closeOthers") {
for (let index = 0; index < tab.parent.children.length; index++) { for (let index = 0; index < tab.parent.children.length; index++) {
if (tab.parent.children[index].id !== tab.id && !tab.parent.children[index].headElement.classList.contains("item--pin")) { if (tab.parent.children[index].id !== tab.id && !tab.parent.children[index].headElement.classList.contains("item--pin")) {
await tab.parent.children[index].parent.removeTab(tab.parent.children[index].id, true, true, false); await tab.parent.children[index].parent.removeTab(tab.parent.children[index].id, true, false);
index--; index--;
} }
} }

View file

@ -378,7 +378,7 @@ export const JSONToLayout = (app: App, isStart: boolean) => {
if (window.siyuan.config.fileTree.closeTabsOnStart && isStart) { if (window.siyuan.config.fileTree.closeTabsOnStart && isStart) {
getAllTabs().forEach(item => { getAllTabs().forEach(item => {
if (item.headElement && !item.headElement.classList.contains("item--pin")) { if (item.headElement && !item.headElement.classList.contains("item--pin")) {
item.parent.removeTab(item.id, false, false, false); item.parent.removeTab(item.id, false, false);
} }
}); });
} }
@ -402,7 +402,7 @@ export const JSONToLayout = (app: App, isStart: boolean) => {
const tabId = item.getAttribute("data-id"); const tabId = item.getAttribute("data-id");
const tab = getInstanceById(tabId) as Tab; const tab = getInstanceById(tabId) as Tab;
if (tab) { if (tab) {
tab.parent.removeTab(tabId, false, false, false); tab.parent.removeTab(tabId, false, false);
} }
} }
} }
@ -643,10 +643,10 @@ export const newModelByInitData = (app: App, tab: Tab, json: any) => {
model = new Editor({ model = new Editor({
app, app,
tab, tab,
rootId: json.rootId,
blockId: json.blockId, blockId: json.blockId,
mode: json.mode, mode: json.mode,
action: typeof json.action === "string" ? [json.action] : json.action, action: typeof json.action === "string" ? [json.action] : json.action,
scrollAttr: json.scrollAttr,
}); });
} }
return model; return model;

View file

@ -27,6 +27,7 @@ import {openDocHistory} from "../../history/doc";
import {openNewWindowById} from "../../window/openNewWindow"; import {openNewWindowById} from "../../window/openNewWindow";
import {genImportMenu} from "../../menus/navigation"; import {genImportMenu} from "../../menus/navigation";
import {transferBlockRef} from "../../menus/block"; import {transferBlockRef} from "../../menus/block";
import {saveScroll} from "../scroll/saveScroll";
export const openTitleMenu = (protyle: IProtyle, position: IPosition) => { export const openTitleMenu = (protyle: IProtyle, position: IPosition) => {
hideTooltip(); hideTooltip();
@ -204,6 +205,7 @@ export const openTitleMenu = (protyle: IProtyle, position: IPosition) => {
label: window.siyuan.languages.openByNewWindow, label: window.siyuan.languages.openByNewWindow,
icon: "iconOpenWindow", icon: "iconOpenWindow",
click() { click() {
saveScroll(protyle);
openNewWindowById(protyle.block.rootID); openNewWindowById(protyle.block.rootID);
} }
}).element); }).element);

View file

@ -39,6 +39,7 @@ import {insertHTML} from "./util/insertHTML";
import {avRender} from "./render/av/render"; import {avRender} from "./render/av/render";
import {focusBlock, getEditorRange} from "./util/selection"; import {focusBlock, getEditorRange} from "./util/selection";
import {hasClosestBlock} from "./util/hasClosest"; import {hasClosestBlock} from "./util/hasClosest";
import {setStorageVal} from "./util/compatibility";
export class Protyle { export class Protyle {
@ -196,7 +197,7 @@ export class Protyle {
setEmpty(app); setEmpty(app);
/// #else /// #else
if (this.protyle.model) { if (this.protyle.model) {
this.protyle.model.parent.parent.removeTab(this.protyle.model.parent.id, false, false); this.protyle.model.parent.parent.removeTab(this.protyle.model.parent.id, false);
} }
/// #endif /// #endif
} }
@ -207,9 +208,11 @@ export class Protyle {
setEmpty(app); setEmpty(app);
/// #else /// #else
if (this.protyle.model) { if (this.protyle.model) {
this.protyle.model.parent.parent.removeTab(this.protyle.model.parent.id, false, false); this.protyle.model.parent.parent.removeTab(this.protyle.model.parent.id, false);
} }
/// #endif /// #endif
delete window.siyuan.storage[Constants.LOCAL_FILEPOSITION][this.protyle.block.rootID];
setStorageVal(Constants.LOCAL_FILEPOSITION, window.siyuan.storage[Constants.LOCAL_FILEPOSITION]);
} }
break; break;
} }
@ -225,10 +228,10 @@ export class Protyle {
removeLoading(this.protyle); removeLoading(this.protyle);
return; return;
} }
if (options.scrollAttr) { if (options.rootId) {
getDocByScroll({ getDocByScroll({
protyle: this.protyle, protyle: this.protyle,
scrollAttr: options.scrollAttr, scrollAttr: window.siyuan.storage[Constants.LOCAL_FILEPOSITION][options.rootId],
mergedOptions, mergedOptions,
cb: () => { cb: () => {
this.afterOnGet(mergedOptions); this.afterOnGet(mergedOptions);
@ -245,19 +248,10 @@ export class Protyle {
this.getDoc(mergedOptions); this.getDoc(mergedOptions);
return; return;
} }
let scrollObj; if (window.siyuan.storage[Constants.LOCAL_FILEPOSITION][response.data.rootID]) {
if (response.data.ial.scroll) {
try {
scrollObj = JSON.parse(response.data.ial.scroll.replace(/&quot;/g, '"'));
} catch (e) {
scrollObj = undefined;
}
}
if (scrollObj) {
scrollObj.rootId = response.data.rootID;
getDocByScroll({ getDocByScroll({
protyle: this.protyle, protyle: this.protyle,
scrollAttr: scrollObj, scrollAttr: window.siyuan.storage[Constants.LOCAL_FILEPOSITION][response.data.rootID],
mergedOptions, mergedOptions,
cb: () => { cb: () => {
this.afterOnGet(mergedOptions); this.afterOnGet(mergedOptions);

View file

@ -39,8 +39,8 @@ export const saveScroll = (protyle: IProtyle, getObject = false) => {
if (getObject) { if (getObject) {
return attr; return attr;
} }
window.siyuan.storage[Constants.LOCAL_FILEPOSITION][protyle.block.rootID] = JSON.stringify(attr); window.siyuan.storage[Constants.LOCAL_FILEPOSITION][protyle.block.rootID] = attr;
setStorageVal(Constants.LOCAL_FILEPOSITION, window.siyuan.storage[Constants.LOCAL_FILEPOSITION][protyle.block.rootID]); setStorageVal(Constants.LOCAL_FILEPOSITION, window.siyuan.storage[Constants.LOCAL_FILEPOSITION]);
}; };
export const getDocByScroll = (options: { export const getDocByScroll = (options: {

View file

@ -33,7 +33,7 @@ export const onGet = (options: {
if (options.data.code === 1) { if (options.data.code === 1) {
// 其他报错 // 其他报错
if (options.protyle.model) { if (options.protyle.model) {
options.protyle.model.parent.parent.removeTab(options.protyle.model.parent.id, false, false); options.protyle.model.parent.parent.removeTab(options.protyle.model.parent.id, false);
} else { } else {
options.protyle.element.innerHTML = `<div class="ft__smaller ft__secondary b3-form__space--small" contenteditable="false">${window.siyuan.languages.refExpired}</div>`; options.protyle.element.innerHTML = `<div class="ft__smaller ft__secondary b3-form__space--small" contenteditable="false">${window.siyuan.languages.refExpired}</div>`;
} }

View file

@ -393,8 +393,8 @@ interface IOptions {
action?: string[], action?: string[],
mode?: TEditorMode, mode?: TEditorMode,
blockId: string blockId: string
rootId?: string
key?: string key?: string
scrollAttr?: IScrollAttr
defId?: string defId?: string
render?: { render?: {
background?: boolean background?: boolean

View file

@ -53,26 +53,16 @@ export const openNewWindowById = (id: string, options: windowOptions = {}) => {
} }
}; };
if (response.data.rootID === id) { if (response.data.rootID === id) {
fetchPost("/api/attr/getBlockAttrs", {id}, (attrResponse) => { /// #if !BROWSER
if (attrResponse.data.scroll) { ipcRenderer.send(Constants.SIYUAN_OPEN_WINDOW, {
json.children.scrollAttr = JSON.parse(attrResponse.data.scroll); position: options.position,
// 历史数据兼容 width: options.width,
json.children.scrollAttr.rootId = response.data.rootID; height: options.height,
} url: `${window.location.protocol}//${window.location.host}/stage/build/app/window.html?v=${Constants.SIYUAN_VERSION}&json=${encodeURIComponent(JSON.stringify(json))}`
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_OPEN_WINDOW, {
position: options.position,
width: options.width,
height: options.height,
url: `${window.location.protocol}//${window.location.host}/stage/build/app/window.html?v=${Constants.SIYUAN_VERSION}&json=${encodeURIComponent(JSON.stringify(json))}`
});
/// #endif
}); });
/// #endif
} else { } else {
json.children.action = Constants.CB_GET_ALL; json.children.action = Constants.CB_GET_ALL;
json.children.scrollAttr = {
zoomInId: id,
};
/// #if !BROWSER /// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_OPEN_WINDOW, { ipcRenderer.send(Constants.SIYUAN_OPEN_WINDOW, {
position: options.position, position: options.position,