Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed May 29, 2023
2 parents c2e15eb + 224490e commit b284b75
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 37 deletions.
150 changes: 119 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ export default class PluginSample extends Plugin {
onload() {
this.data[STORAGE_NAME] = {readonlyText: "Readonly"};

// 图标的制作参见帮助文档
this.addIcons(`<symbol id="iconFace" viewBox="0 0 32 32">
<path d="M13.667 17.333c0 0.92-0.747 1.667-1.667 1.667s-1.667-0.747-1.667-1.667 0.747-1.667 1.667-1.667 1.667 0.747 1.667 1.667zM20 15.667c-0.92 0-1.667 0.747-1.667 1.667s0.747 1.667 1.667 1.667 1.667-0.747 1.667-1.667-0.747-1.667-1.667-1.667zM29.333 16c0 7.36-5.973 13.333-13.333 13.333s-13.333-5.973-13.333-13.333 5.973-13.333 13.333-13.333 13.333 5.973 13.333 13.333zM14.213 5.493c1.867 3.093 5.253 5.173 9.12 5.173 0.613 0 1.213-0.067 1.787-0.16-1.867-3.093-5.253-5.173-9.12-5.173-0.613 0-1.213 0.067-1.787 0.16zM5.893 12.627c2.28-1.293 4.040-3.4 4.88-5.92-2.28 1.293-4.040 3.4-4.88 5.92zM26.667 16c0-1.040-0.16-2.040-0.44-2.987-0.933 0.2-1.893 0.32-2.893 0.32-4.173 0-7.893-1.92-10.347-4.92-1.4 3.413-4.187 6.093-7.653 7.4 0.013 0.053 0 0.12 0 0.187 0 5.88 4.787 10.667 10.667 10.667s10.667-4.787 10.667-10.667z"></path>
</symbol>
<symbol id="iconSaving" viewBox="0 0 32 32">
<path d="M20 13.333c0-0.733 0.6-1.333 1.333-1.333s1.333 0.6 1.333 1.333c0 0.733-0.6 1.333-1.333 1.333s-1.333-0.6-1.333-1.333zM10.667 12h6.667v-2.667h-6.667v2.667zM29.333 10v9.293l-3.76 1.253-2.24 7.453h-7.333v-2.667h-2.667v2.667h-7.333c0 0-3.333-11.28-3.333-15.333s3.28-7.333 7.333-7.333h6.667c1.213-1.613 3.147-2.667 5.333-2.667 1.107 0 2 0.893 2 2 0 0.28-0.053 0.533-0.16 0.773-0.187 0.453-0.347 0.973-0.427 1.533l3.027 3.027h2.893zM26.667 12.667h-1.333l-4.667-4.667c0-0.867 0.12-1.72 0.347-2.547-1.293 0.333-2.347 1.293-2.787 2.547h-8.227c-2.573 0-4.667 2.093-4.667 4.667 0 2.507 1.627 8.867 2.68 12.667h2.653v-2.667h8v2.667h2.68l2.067-6.867 3.253-1.093v-4.707z"></path>
</symbol>`);

const topBarElement = this.addTopBar({
icon: "iconEmoji",
icon: "iconFace",
title: this.i18n.addTopBarIcon,
position: "right",
callback: () => {
this.addMenu(topBarElement.getBoundingClientRect());
let rect = topBarElement.getBoundingClientRect();
// 如果获取不到宽度,则使用更多按钮的宽度
if (rect.width === 0) {
rect = document.querySelector("#barMore").getBoundingClientRect();
}
this.addMenu(rect);
}
});

Expand All @@ -35,7 +48,7 @@ export default class PluginSample extends Plugin {
config: {
position: "LeftBottom",
size: {width: 200, height: 0},
icon: "iconEmoji",
icon: "iconSaving",
title: "Custom Dock",
},
data: {
Expand Down Expand Up @@ -117,7 +130,7 @@ export default class PluginSample extends Plugin {
});
}

private async addMenu(rect: DOMRect) {
private addMenu(rect: DOMRect) {
const menu = new Menu("topBarSample", () => {
console.log(this.i18n.byeMenu);
});
Expand Down Expand Up @@ -150,34 +163,85 @@ export default class PluginSample extends Plugin {
});
}
});
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Tab",
click: () => {
openTab({
custom: {
icon: "iconEmoji",
title: "Custom Tab",
data: {
text: "This is my custom tab",
if (!isMobile()) {
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Custom Tab",
click: () => {
openTab({
app: this.app,
custom: {
icon: "iconFace",
title: "Custom Tab",
data: {
text: "This is my custom tab",
},
fn: this.customTab
},
fn: this.customTab
},
});
}
});
menu.addItem({
icon: "iconLayout",
label: "Open Float Layer(open help)",
click: () => {
this.addFloatLayer({
ids: ["20210428212840-8rqwn5o", "20201225220955-l154bn4"],
defIds: ["20230415111858-vgohvf3", "20200813131152-0wk5akh"],
x: window.innerWidth - 768 - 120,
y: 32
});
}
});
});
}
});
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Asset Tab(open help first)",
click: () => {
openTab({
app: this.app,
asset: {
path: "assets/paragraph-20210512165953-ag1nib4.svg"
}
});
}
});
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Doc Tab(open help first)",
click: () => {
openTab({
app: this.app,
doc: {
id: "20200812220555-lj3enxa",
}
});
}
});
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Search Tab",
click: () => {
openTab({
app: this.app,
search: {
k: "SiYuan"
}
});
}
});
menu.addItem({
icon: "iconLayoutBottom",
label: "Open Card Tab",
click: () => {
openTab({
app: this.app,
card: {
type: "all"
}
});
}
});
menu.addItem({
icon: "iconLayout",
label: "Open Float Layer(open help first)",
click: () => {
this.addFloatLayer({
ids: ["20210428212840-8rqwn5o", "20201225220955-l154bn4"],
defIds: ["20230415111858-vgohvf3", "20200813131152-0wk5akh"],
x: window.innerWidth - 768 - 120,
y: 32
});
}
});
}
menu.addItem({
icon: "iconTrashcan",
label: "Remove Data",
Expand Down Expand Up @@ -239,6 +303,30 @@ export default class PluginSample extends Plugin {
click: () => {
this.eventBus.off("click-editorcontent", this.wsEvent);
}
}, {
icon: "iconSelect",
label: "On click-editortitleicon",
click: () => {
this.eventBus.on("click-editortitleicon", this.wsEvent);
}
}, {
icon: "iconClose",
label: "Off click-editortitleicon",
click: () => {
this.eventBus.off("click-editortitleicon", this.wsEvent);
}
}, {
icon: "iconSelect",
label: "On open-noneditableblock",
click: () => {
this.eventBus.on("open-noneditableblock", this.wsEvent);
}
}, {
icon: "iconClose",
label: "Off open-noneditableblock",
click: () => {
this.eventBus.off("open-noneditableblock", this.wsEvent);
}
}]
});
menu.addSeparator();
Expand Down
71 changes: 65 additions & 6 deletions src/siyuan.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
type TEventBus = "ws-main" | "click-blockicon" | "click-editorcontent" | "click-pdf" | "click-editortitleicon"
type TEventBus = "ws-main" | "click-blockicon" | "click-editorcontent" | "click-pdf" |
"click-editortitleicon" | "open-noneditableblock"

type TCardType = "doc" | "notebook" | "all"

declare global {
interface Window {
Expand All @@ -20,6 +23,31 @@ interface ILuteNode {
};
}

interface ISearchOption {
page?: number
group?: number, // 0:不分组,1:按文档分组
hasReplace?: boolean,
method?: number // 0:文本,1:查询语法,2:SQL,3:正则表达式
hPath?: string
idPath?: string[]
k: string
r?: string
types?: {
mathBlock: boolean
table: boolean
blockquote: boolean
superBlock: boolean
paragraph: boolean
document: boolean
heading: boolean
list: boolean
listItem: boolean
codeBlock: boolean
htmlBlock: boolean
embedBlock: boolean
}
}

interface IWebSocketData {
cmd: string
callback?: string
Expand All @@ -35,6 +63,8 @@ declare interface IPluginDockTab {
icon: string,
hotkey?: string,
title: string,
index?: number,
show?: boolean
}

interface IMenuItemOption {
Expand All @@ -51,6 +81,7 @@ interface IMenuItemOption {
current?: boolean
bind?: (element: HTMLElement) => void
index?: number
element?: HTMLElement
}

export function fetchPost(url: string, data?: any, cb?: (response: IWebSocketData) => void, headers?: IObject): void;
Expand All @@ -60,12 +91,32 @@ export function fetchSyncPost(url: string, data?: any): Promise<IWebSocketData>;
export function fetchGet(url: string, cb: (response: IWebSocketData) => void): void;

export function openTab(options: {
app: App,
doc?: {
id: string, // 块 id
action?: string [] // cb-get-all:获取所有内容;cb-get-focus:打开后光标定位在 id 所在的块;cb-get-hl: 打开后 id 块高亮
zoomIn?: boolean // 是否缩放
},
pdf?: {
path: string,
page?: number, // pdf 页码
id?: string, // File Annotation id
},
asset?: {
path: string,
},
search?: ISearchOption
card?: {
type: TCardType,
id?: string, // cardType 为 all 时不传,否则传文档或笔记本 id
title?: string // cardType 为 all 时不传,否则传文档或笔记本名称
},
custom?: {
title: string,
icon: string,
data?: any
fn?: () => any,
} // card 和自定义页签 必填
}
position?: "right" | "bottom",
keepCursor?: boolean // 是否跳转到新 tab 上
removeCurrentTab?: boolean // 在当前页签打开时需移除原有页签
Expand Down Expand Up @@ -93,6 +144,7 @@ export abstract class Plugin {
i18n: IObject;
data: any;
name: string;
app: App;

constructor(options: {
app: App,
Expand All @@ -107,7 +159,8 @@ export abstract class Plugin {

onLayoutReady(): void;

/*
/**
* Must be executed before the synchronous function.
* @param {string} [options.position=right]
*/
addTopBar(options: {
Expand All @@ -121,14 +174,17 @@ export abstract class Plugin {

// registerCommand(command: IPluginCommand): void;

// registerSettingRender(settingRender: SettingRender): void;

loadData(storageName: string): Promise<any>;

saveData(storageName: string, content: any): Promise<void>;

removeData(storageName: string): Promise<any>;

addIcons(svg: string): void;

/**
* Must be executed before the synchronous function.
*/
addTab(options: {
type: string,
destroy?: () => void,
Expand All @@ -137,6 +193,9 @@ export abstract class Plugin {
init: () => void
}): () => any

/**
* Must be executed before the synchronous function.
*/
addDock(options: {
config: IPluginDockTab,
data: any,
Expand Down Expand Up @@ -197,7 +256,7 @@ export class Menu {

open(options: { x: number, y: number, h?: number, w?: number, isLeft?: boolean }): void;

/*
/**
* @param {string} [position=all]
*/
fullscreen(position?: "bottom" | "all"): void;
Expand Down

0 comments on commit b284b75

Please sign in to comment.