Skip to content

Commit

Permalink
feat: 兼容 siyuanhook
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 14, 2023
1 parent 2c78db5 commit a0017c3
Show file tree
Hide file tree
Showing 17 changed files with 365 additions and 4 deletions.
Binary file modified dependency-graph.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion libs/publisher-bridge/src/publish-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* questions.
*/

import PublishHook from "@terwer/publisher-hook"
import { PublishHook } from "@terwer/publisher-hook"
import { Env } from "zhi-env"
import { LogFactory, LogLevelEnum, DefaultLogger } from "zhi-log"
import { SiyuanDevice } from "zhi-device"
Expand Down
3 changes: 1 addition & 2 deletions libs/publisher-hook/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import PublishHook from "./publish-hook"
import { SiyuanDevice, BrowserUtil, DeviceDetection, DeviceTypeEnum } from "zhi-device"

export default PublishHook
export { SiyuanDevice, BrowserUtil, DeviceDetection, DeviceTypeEnum }
export { PublishHook, SiyuanDevice, BrowserUtil, DeviceDetection, DeviceTypeEnum }
1 change: 1 addition & 0 deletions libs/siyuan-hook/.env.development.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_DEBUG_MODE=true
4 changes: 4 additions & 0 deletions libs/siyuan-hook/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ["./node_modules/@terwer/eslint-config-custom/typescript/index.cjs"],
}
2 changes: 2 additions & 0 deletions libs/siyuan-hook/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.DS_Store
11 changes: 11 additions & 0 deletions libs/siyuan-hook/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# platform

# Ignore artifacts:
dist
node_modules

# Ignore all dts files:
*.d.ts

# lib
/pnpm-lock.yaml
30 changes: 30 additions & 0 deletions libs/siyuan-hook/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

module.exports = {
semi: false,
singleQuote: false,
printWidth: 120
}
41 changes: 41 additions & 0 deletions libs/siyuan-hook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# siyuan-hook

siyuanhook for sy-post-publisher

## Usage

```js
// usage
```

## Deps

```
## Congregations! siyuan-hook need no deps, it is just pure js code 🎉
```

## Dev

```bash
pnpm dev -F siyuan-hook
```

## Build

```bash
pnpm build -F siyuan-hook
```

## Test

Execute the unit tests via [vitest](https://vitest.dev)

```bash
pnpm test -F siyuan-hook
```

## Publish

```bash
pnpm publish -F siyuan-hook --tag latest
```
12 changes: 12 additions & 0 deletions libs/siyuan-hook/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue + TS</title>
</head>
<body>
This file is for lib hot-load test only, see <a href="/src/index.ts">/src/index.ts</a>
<script type="module" src="/src/index.ts"></script>
</body>
</html>
32 changes: 32 additions & 0 deletions libs/siyuan-hook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "siyuan-hook",
"version": "0.0.1",
"type": "module",
"description": "siyuanhook for sy-post-publisher",
"repository": "terwer/siyuan-plugin-publisher",
"homepage": "https://github.com/terwer/siyuan-plugin-publisher/tree/main/libs/siyuan-hook",
"author": "terwer",
"license": "GPL",
"files": [
"dist",
"README.md"
],
"keywords": [
"zhi",
"lib"
],
"scripts": {
"serve": "vite",
"dev": "vite build --watch",
"build": "vite build",
"start": "vite preview",
"test": "vitest --watch"
},
"devDependencies": {
"@terwer/eslint-config-custom": "^1.2.0",
"@terwer/vite-config-custom": "^0.2.0"
},
"dependencies": {
"@terwer/publisher-hook": "workspace:*"
}
}
123 changes: 123 additions & 0 deletions libs/siyuan-hook/src/siyuanhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

// 警告1⚠️:请勿在非思源笔记浏览器环境调用此文件中的任何方法
// 警告2⚠️:此文件请勿引用其他任何需要编译的类库
// 提示3⚠️:此文件是挂件唯一的hook入口

import { PublishHook, DeviceDetection, BrowserUtil, DeviceTypeEnum } from "@terwer/publisher-hook"

// 统一的初始化入口
;(async () => {
console.warn(
"从 0.6.8+ 开始,siyuanhook.js 将作为挂件统一的 hook 入口。" +
"当前 siyuanhook.js 源码已迁移至:https://github.com/terwer/siyuan-plugin-publisher/tree/main/libs/siyuan-hook 统一管理。"
)
try {
const device = DeviceDetection.getDevice()
const publisherHook = new PublishHook()

// init
const init = async () => {
// Electron里面有3种情况
// 1、js片段
// 2、iframe挂件
// 3、新窗口打开
const isElectron = BrowserUtil.isElectron()
if (!isElectron) {
console.warn("不在Electron环境中,插槽菜单、JsonLocalStorage等将不可用")
return
}

// iframe挂件
const initIframeWidget = () => {
publisherHook.doInit({
// 挂载JsonLocalStorage到window
isInitLocalStorage: true,
// 初始化插槽
isInitSlot: true,
// 初始化主题适配
isInitThemeAdaptor: true,
// 初始化发布辅助功能
isInitPublishHelper: true,
// 初始化PicGO配置
isInitPicgoExtension: true,
// 初始化SyCmd配置
isInitCmder: true,
})
}

// 新窗口打开
const initSiyuanNewWin = () => {
publisherHook.doInit({
// 挂载JsonLocalStorage到window
isInitLocalStorage: true,
// 初始化发布辅助功能
isInitPublishHelper: true,
// 初始化PicGO配置
isInitPicgoExtension: true,
// 初始化SyCmd配置
isInitCmder: true,
})
}

// js片段
const initJsCode = () => {
publisherHook.doInit({
// 挂载JsonLocalStorage到window
isInitLocalStorage: true,
// 初始化插槽
isInitSlot: true,
// 初始化主题适配
isInitThemeAdaptor: true,
// 初始化发布辅助功能
isInitPublishHelper: true,
// 初始化PicGO配置
isInitPicgoExtension: true,
// 初始化SyCmd配置
isInitCmder: true,
})
}

if (device == DeviceTypeEnum.DeviceType_Siyuan_Widget) {
// iframe挂件
initIframeWidget()
} else {
if (device == DeviceTypeEnum.DeviceType_Siyuan_NewWin) {
// 新窗口打开
initSiyuanNewWin()
} else {
// 自定义js片段
initJsCode()
}
}
}

// 启动入口
await init()
} catch (e) {
console.warn("初始化siyuanhook失败,可能导致扩展功能无法使用,请知悉。错误信息如下", e)
}
})()
42 changes: 42 additions & 0 deletions libs/siyuan-hook/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": [
"ES2020",
"DOM",
"DOM.Iterable"
],
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "Node",
// "allowImportingTsExtensions": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"types": [
"node",
"vite/client"
]
},
"include": [
"src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx",
"src/**/*.vue"
],
"references": [
{
"path": "./tsconfig.node.json"
}
],
"root": "."
}
10 changes: 10 additions & 0 deletions libs/siyuan-hook/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
40 changes: 40 additions & 0 deletions libs/siyuan-hook/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// <reference types="vitest" />

import { resolve } from "path"
import { defineConfig } from "vite"
import minimist from "minimist";

const args = minimist(process.argv.slice(2))
const isWatch = args.watch || args.w
const devDistDir = "/Users/terwer/Documents/mydocs/siyuan-widgets/sy-post-publisher/public/lib"
const distDir = isWatch ? devDistDir : "./dist"

export default defineConfig({
build: {
// 输出路径
outDir: distDir,

lib: {
// Could also be a dictionary or array of multiple entry points
entry: resolve(__dirname, "src/siyuanhook.ts"),
// the proper extensions will be added
fileName: "siyuanhook",
formats: ["cjs"],
},
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
// into your library
external: [],
output: {
entryFileNames: "[name].js",
},
},
minify: false,
},

test: {
globals: true,
environment: "jsdom",
include: ["src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
},
})
3 changes: 2 additions & 1 deletion plugins/publisher-main/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ export default class PublishTool extends Plugin {
// const widgetId = "1111111"
// const pageUrl = "detail/index.html"

win.syp.renderPublishHelper(pageId, pageUrl, win, this.env.isDev())
// win.syp.renderPublishHelper(pageId, pageUrl, win, this.env.isDev())
win.syp.renderPublishHelper(pageId, pageUrl, win, true)
console.log("publisherHook inited")
})
})
Expand Down

0 comments on commit a0017c3

Please sign in to comment.