Skip to content

Commit 2805cdd

Browse files
committed
feat: 支持移动端
1 parent 885b2f6 commit 2805cdd

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

src/index.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import {
55
fetchSyncPost,
66
Wnd,
77
Layout,
8-
Tab
8+
Tab,
9+
getFrontend
910
} from "siyuan";
1011
import "./index.scss";
1112

1213
export default class PluginSample extends Plugin {
14+
isMobile: boolean;
15+
1316
onload() {
17+
const frontEnd = getFrontend();
18+
this.isMobile = frontEnd === "mobile" || frontEnd === "browser-mobile";
1419
// "open-menu-doctree": {
1520
// menu: subMenu,
1621
// elements: NodeListOf<HTMLElement>,
@@ -109,31 +114,40 @@ export default class PluginSample extends Plugin {
109114
// };
110115
// TODO跟进: 原生函数获取当前文档 ID https://github.com/siyuan-note/siyuan/issues/15415
111116
getCurrentDocParams = (): { path: string, notebookId: string } | false => {
112-
let element = document.querySelector(".layout__wnd--active > .fn__flex > .layout-tab-bar > .item--focus") as HTMLElement;
113-
if (!element) {
114-
document.querySelectorAll("ul.layout-tab-bar > .item--focus").forEach((item: HTMLElement, index) => {
115-
if (index === 0) {
116-
element = item;
117-
} else if (item.dataset.activetime > element.dataset.activetime) {
118-
element = item;
119-
}
120-
});
121-
}
117+
if (this.isMobile) {
118+
if (window.siyuan.mobile.editor && window.siyuan.mobile.editor.protyle) {
119+
return {
120+
path: window.siyuan.mobile.editor.protyle.path,
121+
notebookId: window.siyuan.mobile.editor.protyle.notebookId
122+
};
123+
}
124+
} else {
125+
let element = document.querySelector(".layout__wnd--active > .fn__flex > .layout-tab-bar > .item--focus") as HTMLElement;
126+
if (!element) {
127+
document.querySelectorAll("ul.layout-tab-bar > .item--focus").forEach((item: HTMLElement, index) => {
128+
if (index === 0) {
129+
element = item;
130+
} else if (item.dataset.activetime > element.dataset.activetime) {
131+
element = item;
132+
}
133+
});
134+
}
122135

123-
if (element) {
124-
const tab = this.getInstanceById(element.getAttribute("data-id")) as Tab;
125-
// 页签有可能不是文档页签
126-
if (tab && tab.model && typeof tab.model === 'object' && 'editor' in tab.model) {
127-
const model = tab.model as any;
128-
if (model.editor && model.editor.protyle) {
129-
return {
130-
path: model.editor.protyle.path,
131-
notebookId: model.editor.protyle.notebookId
132-
};
136+
if (element) {
137+
const tab = this.getInstanceById(element.getAttribute("data-id")) as Tab;
138+
// 页签有可能不是文档页签
139+
if (tab && tab.model && typeof tab.model === 'object' && 'editor' in tab.model) {
140+
const model = tab.model as any;
141+
if (model.editor && model.editor.protyle) {
142+
return {
143+
path: model.editor.protyle.path,
144+
notebookId: model.editor.protyle.notebookId
145+
};
146+
}
133147
}
134148
}
149+
return false;
135150
}
136-
return false;
137151
};
138152

139153
// 原生方法,不保证未来一直兼容

webpack.config.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ module.exports = (env, argv) => {
2020
entry = {
2121
"dist/index": "./src/index.ts",
2222
};
23-
plugins.push(new webpack.BannerPlugin({
24-
banner: () => {
25-
return fs.readFileSync("LICENSE").toString();
26-
},
27-
}));
2823
plugins.push(new CopyPlugin({
2924
patterns: [
3025
{from: "preview.png", to: "./dist/"},

0 commit comments

Comments
 (0)