Skip to content

Commit f2d0152

Browse files
committed
🎨 fix #8944
1 parent 732c080 commit f2d0152

File tree

6 files changed

+49
-34
lines changed

6 files changed

+49
-34
lines changed

app/src/boot/onGetConfig.ts

+14-19
Original file line numberDiff line numberDiff line change
@@ -259,25 +259,20 @@ export const initWindow = (app: App) => {
259259
// siyuan://plugins/plugin-name/foo?bar=baz
260260
plugin.eventBus.emit("open-siyuan-url-plugin", {url});
261261
// siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."}
262-
Object.keys(plugin.models).find(key => {
263-
if (key === pluginId) {
264-
let data = getSearch("data", url);
265-
try {
266-
data = JSON.parse(data || "{}");
267-
} catch (e) {
268-
console.log("Error open plugin tab with protocol:", e);
269-
}
270-
openFile({
271-
app,
272-
custom: {
273-
title: getSearch("title", url),
274-
icon: getSearch("icon", url),
275-
data,
276-
fn: plugin.models[key]
277-
},
278-
});
279-
return true;
280-
}
262+
let data = getSearch("data", url);
263+
try {
264+
data = JSON.parse(data || "{}");
265+
} catch (e) {
266+
console.log("Error open plugin tab with protocol:", e);
267+
}
268+
openFile({
269+
app,
270+
custom: {
271+
title: getSearch("title", url),
272+
icon: getSearch("icon", url),
273+
data,
274+
id: pluginId
275+
},
281276
});
282277
return true;
283278
}

app/src/card/openCard.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export const bindCardEvent = (options: {
221221
id: filterElement.getAttribute("data-id"),
222222
title: options.title
223223
},
224-
fn: newCardModel
224+
id: "siyuan-card"
225225
},
226226
});
227227
if (options.dialog) {

app/src/editor/util.ts

+28-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {objEquals} from "../util/functions";
2828
import {resize} from "../protyle/util/resize";
2929
import {Search} from "../search";
3030
import {App} from "../index";
31+
import {newCardModel} from "../card/newCardTab";
3132

3233
export const openFileById = async (options: {
3334
app: App,
@@ -100,7 +101,7 @@ export const openFile = (options: IOpenFileOptions) => {
100101
}
101102
} else if (options.custom) {
102103
const custom = allModels.custom.find((item) => {
103-
if (objEquals(item.data, options.custom.data)) {
104+
if (objEquals(item.data, options.custom.data) && (!options.custom.id || options.custom.id === item.type)) {
104105
if (!pdfIsLoading(item.parent.parent.element)) {
105106
item.parent.parent.switchTab(item.parent.headElement);
106107
item.parent.parent.showHeading();
@@ -427,11 +428,32 @@ const newTab = (options: IOpenFileOptions) => {
427428
icon: options.custom.icon,
428429
title: options.custom.title,
429430
callback(tab) {
430-
tab.addModel(options.custom.fn({
431-
app: options.app,
432-
tab,
433-
data: options.custom.data
434-
}));
431+
if (options.custom.id) {
432+
if (options.custom.id === "siyuan-card") {
433+
tab.addModel(newCardModel({
434+
app: options.app,
435+
tab,
436+
data: options.custom.data
437+
}));
438+
} else {
439+
options.app.plugins.find(p => {
440+
if (p.models[options.custom.id]) {
441+
tab.addModel(p.models[options.custom.id]({
442+
tab,
443+
data: options.custom.data
444+
}));
445+
return true;
446+
}
447+
})
448+
}
449+
} else {
450+
// plugin 0.8.3 历史兼容
451+
console.warn("0.8.3 将移除 custom.fn 参数,请参照 https://github.com/siyuan-note/plugin-sample/blob/91a716358941791b4269241f21db25fd22ae5ff5/src/index.ts 将其修改为 custom.id");
452+
tab.addModel(options.custom.fn({
453+
tab,
454+
data: options.custom.data
455+
}));
456+
}
435457
setPanelFocus(tab.panelElement.parentElement.parentElement);
436458
}
437459
});

app/src/layout/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ export const JSONToLayout = (app: App, isStart: boolean) => {
412412
const initData = item.getAttribute("data-initdata");
413413
if (initData) {
414414
const initDataObj = JSON.parse(initData);
415-
if (initDataObj.instance === "Custom") {
415+
if (initDataObj.instance === "Custom" && initDataObj.customModelType !== "siyuan-card") {
416416
let hasPlugin = false;
417417
app.plugins.find(plugin => {
418418
if (Object.keys(plugin.models).includes(initDataObj.customModelType)) {

app/src/plugin/API.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import {getBackend, getFrontend} from "../util/functions";
88
import {openFile, openFileById} from "../editor/util";
99
/// #endif
1010
import {updateHotkeyTip} from "../protyle/util/compatibility";
11-
import {newCardModel} from "../card/newCardTab";
1211
import {App} from "../index";
1312
import {Constants} from "../constants";
14-
import {Model} from "../layout/Model";
1513
import {Setting} from "./Setting";
1614
import {Menu} from "./Menu";
17-
import { Protyle } from "../protyle";
15+
import {Protyle} from "../protyle";
1816

1917
let openTab;
2018
/// #if MOBILE
@@ -47,7 +45,7 @@ openTab = (options: {
4745
title: string,
4846
icon: string,
4947
data?: any
50-
fn?: () => Model,
48+
id: string
5149
}
5250
position?: "right" | "bottom",
5351
keepCursor?: boolean // 是否跳转到新 tab 上
@@ -128,7 +126,7 @@ openTab = (options: {
128126
id: options.card.id || "",
129127
title: options.card.title,
130128
},
131-
fn: newCardModel
129+
id: "siyuan-card"
132130
},
133131
});
134132
}

app/src/types/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,11 @@ interface IOpenFileOptions {
437437
title: string,
438438
icon: string,
439439
data?: any
440+
id: string,
440441
fn?: (options: {
441442
tab: import("../layout/Tab").Tab,
442443
data: any,
443-
app: import("../index").App
444-
}) => import("../layout/Model").Model,
444+
}) => import("../layout/Model").Model, // plugin 0.8.3 历史兼容
445445
}
446446
assetPath?: string, // asset 必填
447447
fileName?: string, // file 必填

0 commit comments

Comments
 (0)