Skip to content

Commit

Permalink
feat(webview): 在新窗口打开的思源编辑器中添加 Electron 上下文 | Add Electron contex…
Browse files Browse the repository at this point in the history
…t to SiYuan Editor opened in a new window.
  • Loading branch information
Zuoqiu-Yingyi committed May 27, 2023
1 parent 009d4c4 commit 1b939ed
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 38 deletions.
32 changes: 32 additions & 0 deletions public/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div align="center">
<img alt="icon" src="./icon.png" style="width: 8em; height: 8em;">

---
[![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/Zuoqiu-Yingyi/siyuan-plugin-webview?include_prereleases&style=flat-square)](https://github.com/Zuoqiu-Yingyi/siyuan-plugin-webview/releases/latest)
[![GitHub Release Date](https://img.shields.io/github/release-date/Zuoqiu-Yingyi/siyuan-plugin-webview?style=flat-square)](https://github.com/Zuoqiu-Yingyi/siyuan-plugin-webview/releases/latest)
[![GitHub License](https://img.shields.io/github/license/Zuoqiu-Yingyi/siyuan-plugin-webview?style=flat-square)](https://github.com/Zuoqiu-Yingyi/siyuan-plugin-webview/blob/main/LICENSE)
[![GitHub last commit](https://img.shields.io/github/last-commit/Zuoqiu-Yingyi/siyuan-plugin-webview?style=flat-square)](https://github.com/Zuoqiu-Yingyi/siyuan-plugin-webview/commits/main)
![GitHub repo size](https://img.shields.io/github/repo-size/Zuoqiu-Yingyi/siyuan-plugin-webview?style=flat-square)
![hits](https://hits.b3log.org/Zuoqiu-Yingyi/siyuan-plugin-webview.svg)
[![GitHub all releases](https://img.shields.io/github/downloads/Zuoqiu-Yingyi/siyuan-plugin-webview/total?style=flat-square)](https://github.com/Zuoqiu-Yingyi/siyuan-plugin-webview/releases)

---
[简体中文](./README_zh_CN.md) \| English

---
</div>

# SiYuan Webview

An [SiYuan](https://github.com/siyuan-note/siyuan) plugin that allows you to browse the web within tabs or new window.

## PREVIEW

![preview image](./preview.png)

## INTRODUCTION


## CHANGELOG

[CHANGELOG.md](https://github.com/Zuoqiu-Yingyi/siyuan-plugin-webview/blob/main/CHANGELOG.md)
Binary file added public/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 33 additions & 13 deletions src/components/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
},
];
let tab_tabs_focus_key = TabKey.general;
let window_tabs_focus_key = TabKey.general;
let tab_settings_tabs_focus_key = TabKey.general;
let window_settings_tabs_focus_key = TabKey.general;
const tabs = {
tab: [
{
Expand All @@ -116,6 +116,26 @@
name: i18n.settings.shortcut,
icon: "",
},
] as ITab[],
window: [
{
key: TabKey.general,
text: i18n.settings.general,
name: i18n.settings.general,
icon: "",
},
{
key: TabKey.protocol,
text: i18n.settings.protocol,
name: i18n.settings.protocol,
icon: "🌐",
},
{
key: TabKey.shortcut,
text: i18n.settings.shortcut,
name: i18n.settings.shortcut,
icon: "",
},
{
key: TabKey.siyuan,
text: i18n.settings.siyuan.title,
Expand Down Expand Up @@ -171,7 +191,7 @@
<!-- 打开页签的设置面板 -->
<Panel display={panels[1].key === focusPanel}>
<Tabs
focus={tab_tabs_focus_key}
focus={tab_settings_tabs_focus_key}
tabs={tabs.tab}
let:focus={focusTab}
>
Expand Down Expand Up @@ -279,14 +299,14 @@
<!-- 打开窗口的设置面板 -->
<Panel display={panels[2].key === focusPanel}>
<Tabs
focus={window_tabs_focus_key}
tabs={tabs.tab}
focus={window_settings_tabs_focus_key}
tabs={tabs.window}
let:focus={focusTab}
>
<!-- 标签页 1 - 通用设置 -->
<div
data-type={tabs.tab[0].name}
class:fn__none={tabs.tab[0].key !== focusTab}
data-type={tabs.window[0].name}
class:fn__none={tabs.window[0].key !== focusTab}
>
<!-- 是否启用 -->
<Item
Expand Down Expand Up @@ -433,8 +453,8 @@

<!-- 标签页 2 - 协议设置 -->
<div
data-type={tabs.tab[1].name}
class:fn__none={tabs.tab[1].key !== focusTab}
data-type={tabs.window[1].name}
class:fn__none={tabs.window[1].key !== focusTab}
>
<Group title={i18n.settings.protocols.title}>
{#each Object.entries(config.window.open.protocols) as [key, protocol] (key)}
Expand All @@ -460,8 +480,8 @@

<!-- 标签页 3 - 快捷键设置 -->
<div
data-type={tabs.tab[2].name}
class:fn__none={tabs.tab[2].key !== focusTab}
data-type={tabs.window[2].name}
class:fn__none={tabs.window[2].key !== focusTab}
>
<Shortcut
minWidth="16em"
Expand All @@ -477,8 +497,8 @@

<!-- 标签页 4 - 思源窗口设置 -->
<div
data-type={tabs.tab[3].name}
class:fn__none={tabs.tab[3].key !== focusTab}
data-type={tabs.window[3].name}
class:fn__none={tabs.window[3].key !== focusTab}
>
<!-- 是否启用 -->
<Item
Expand Down
2 changes: 1 addition & 1 deletion src/configs/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const DEFAULT_CONFIG: IConfig = {
enable: true,
},
other: {
enable: true,
enable: false,
},
}
},
Expand Down
53 changes: 33 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default class WebviewPlugin extends siyuan.Plugin {

public openWindow(
href: string,
params: IOverwrite | IWindowParams = {
params: IOverwrite = {
x: 0,
y: 0,
title: null,
Expand All @@ -189,22 +189,35 @@ export default class WebviewPlugin extends siyuan.Plugin {
}
}

public openSiyuanDesktopWindow(e?: MouseEvent): void {
public openSiyuanDesktopWindow(e?: MouseEvent, href?: string): void {
const params = {
x: e?.screenX | 0,
y: e?.screenY | 0,
title: "desktop",
alwaysOnTop: false, // 桌面端禁用置顶
autoHideMenuBar: false, // 禁用自动隐藏菜单栏
webPreferences: {
nodeIntegration: true, // 是否启用 Node.js 内置模块
webviewTag: true, // 是否启用 webview 标签
contextIsolation: false, // 是否开启上下文隔离, 设置 false 之后可以使用 require
},

enableMenuBar: true, // (自定义) 启用菜单栏
enableElectron: true, // (自定义) 启用 Electron 环境
}
this.openWindow(buildSiyuanWebURL(Pathname.desktop).href, params);
this.openWindow(href || buildSiyuanWebURL(Pathname.desktop).href, params);
}

public openSiyuanMobileWindow(e?: MouseEvent): void {
public openSiyuanMobileWindow(e?: MouseEvent, href?: string): void {
const params = {
x: e?.screenX | 0,
y: e?.screenY | 0,
title: "desktop",
title: "mobile",
alwaysOnTop: false, // 移动端启用置顶
enableMenuBar: true, // 启用菜单栏
autoHideMenuBar: false, // 禁用自动隐藏菜单栏
}
this.openWindow(buildSiyuanWebURL(Pathname.mobile).href, params);
this.openWindow(href || buildSiyuanWebURL(Pathname.mobile).href, params);
}

protected isUrlSchemeAvailable(url: string, protocols: IProtocols): boolean {
Expand Down Expand Up @@ -320,22 +333,22 @@ export default class WebviewPlugin extends siyuan.Plugin {
e.preventDefault();
e.stopPropagation();

this.openWindow(
buildSiyuanWebURL(
editorType2Pathname(this.config.window.siyuan.editorType),
{
id: block_id,
focus: this.config.window.siyuan.focus,
}
).href,
const url = buildSiyuanWebURL(
editorType2Pathname(this.config.window.siyuan.editorType),
{
x: e.screenX,
y: e.screenY,
title: undefined,
enableMenuBar: true,
autoHideMenuBar: false,
},
id: block_id,
focus: this.config.window.siyuan.focus,
}
);
switch (this.config.window.siyuan.editorType) {
case EditorType.desktop:
this.openSiyuanDesktopWindow(e, url.href);
break;

case EditorType.mobile:
this.openSiyuanMobileWindow(e, url.href);
break;
}
} catch (e) {
this.logger.warn(e);
}
Expand Down
3 changes: 2 additions & 1 deletion src/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ export interface IWindowParams extends Electron.BrowserWindowConstructorOptions
frame: boolean, // 窗口边框
alwaysOnTop: boolean, // 是否置顶
autoHideMenuBar: boolean, // 是否自动隐藏菜单栏

enableMenuBar: boolean, // 是否启用菜单栏
}

export interface ISiyuanWindow {
export interface ISiyuanWindow {
enable: boolean, // 是否打开思源编辑窗口
focus: boolean, // 打开的思源编辑窗口是否默认聚焦
editorType: EditorType.desktop | EditorType.mobile, // 打开的思源编辑器类型
Expand Down
17 changes: 14 additions & 3 deletions src/utils/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export enum MenuBarStatus {

/* 页面配置 */
export interface IWebPreferences extends Electron.WebPreferences {
defaultFontSize: number, // 默认字体大小
defaultFontFamily: {
standard: string, // 默认字体
defaultFontSize?: number, // 默认字体大小
defaultFontFamily?: {
standard?: string, // 默认字体
},
}

Expand All @@ -46,6 +46,9 @@ export interface IOverwrite extends Electron.BrowserWindowConstructorOptions {
y: number, // 窗口纵坐标
title: string, // 窗口标题
webPreferences?: IWebPreferences, // 页面配置

enableMenuBar?: boolean, // (自定义) 是否启用菜单栏
enableElectron?: boolean, // (自定义) 是否启用 Electron 环境
}

export function openNewWindow(
Expand Down Expand Up @@ -73,6 +76,14 @@ export function openNewWindow(
window.removeMenu();
}

/* 是否启用 Electron 环境 */
if (params.enableElectron) {
globalThis
.require('@electron/remote')
.require('@electron/remote/main')
.enable(window.webContents);
}

/* 加载 URL */
window.loadURL(
url.href,
Expand Down

0 comments on commit 1b939ed

Please sign in to comment.