Skip to content

Commit

Permalink
Merge pull request #122 from terwer/dev
Browse files Browse the repository at this point in the history
feat: PC客户端支持切换ip [实验性]
  • Loading branch information
terwer committed Jun 25, 2023
2 parents 363bf1d + de35c96 commit fd5eace
Show file tree
Hide file tree
Showing 22 changed files with 171 additions and 62 deletions.
2 changes: 1 addition & 1 deletion components/default/Detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ await setCurrentPost(props.pageId)
// datas
const attrs = JsonUtil.safeParse<any>(currentPost.post?.attrs ?? "{}", {})
const shareEnabled = attrs["custom-publish-status"] === "publish"
const shareEnabled = attrs["custom-publish-status"] === "publish" || attrs["custom-publish-status"] === "preview"
const isExpires = checkExpires(attrs)
logger.info(`current document status,shareEnabled => ${shareEnabled}, isExpires => ${isExpires}`)
if (!props.overrideSeo) {
Expand Down
1 change: 0 additions & 1 deletion components/default/Footer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { version } from "~/package.json"
import { DateUtil } from "zhi-common"
import { useI18n } from "vue-i18n"
import { useThemeMode } from "~/composables/useThemeMode"
import { useSiyuanDevice } from "~/composables/useSiyuanDevice"
import { useBaseUrl } from "~/plugins/renderer/useClientBaseUrl"
// uses
Expand Down
5 changes: 2 additions & 3 deletions composables/api/usePostApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@

import { createAppLogger } from "~/common/appLogger"
import { SiYuanApiAdaptor, SiyuanConfig } from "zhi-siyuan-api"
import { useSiyuanApi } from "~/composables/api/useSiyuanApi"

/**
* 文档相关
*/
export const usePostApi = () => {
const logger = createAppLogger("use-post")
const env = useRuntimeConfig()
const { blogApi } = useSiyuanApi()

const getPost = async (id: string, useSlug?: boolean, skipBody?: boolean) => {
logger.info("Loading post from remote api...")
Expand All @@ -41,8 +42,6 @@ export const usePostApi = () => {
// logger.info("siyuanApiUrl=>", env.public.siyuanApiUrl)
// logger.info("siyuanAuthToken=>", env.siyuanAuthToken)

const siyuanConfig = new SiyuanConfig(env.public.siyuanApiUrl, env.siyuanAuthToken)
const blogApi = new SiYuanApiAdaptor(siyuanConfig)
const postid = id.replace(/\.html$/, "")
return await blogApi.getPost(postid, useSlug, skipBody)
}
Expand Down
17 changes: 17 additions & 0 deletions composables/api/useSiyuanApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,36 @@
*/

import { SiYuanApiAdaptor, SiyuanConfig, SiyuanKernelApi } from "zhi-siyuan-api"
import { createAppLogger } from "~/common/appLogger"

/**
* 通用 Siyuan API 封装
*/
export const useSiyuanApi = () => {
const logger = createAppLogger("use-siyuan-api")
const env = useRuntimeConfig()

const siyuanConfig = new SiyuanConfig(env.public.siyuanApiUrl, env.siyuanAuthToken)
siyuanConfig.cookie = env.siyuanCookie
const blogApi = new SiYuanApiAdaptor(siyuanConfig)
const kernelApi = new SiyuanKernelApi(siyuanConfig)

const isStorageViaSiyuanApi = () => {
// docker - 在 .env.docker 配置 NUXT_PUBLIC_DEFAULT_TYPE=siyuan
// vercel - 在环境变量配置 NUXT_PUBLIC_DEFAULT_TYPE=siyuan
// node - 启动参数加 NUXT_PUBLIC_DEFAULT_TYPE=siyuan node NUXT_PUBLIC_SIYUAN_API_URL=http://127.0.0.1:6806
// 插件SPA(PC客户端) - nuxt.siyuan.config.ts 写死 NUXT_PUBLIC_DEFAULT_TYPE: siyuan
// 插件SPA(Docker浏览器客户端) - nuxt.siyuan.config.ts 写死 NUXT_PUBLIC_DEFAULT_TYPE: siyuan
// 插件SPA(本地客户端浏览器) - nuxt.siyuan.config.ts 写死 NUXT_PUBLIC_DEFAULT_TYPE: siyuan
const storeViaSiyuanApi = env.public.defaultType === "siyuan"
logger.info("defaultType=>", env.public.defaultType)
logger.info("storeViaSiyuanApi=>", String(storeViaSiyuanApi))
return storeViaSiyuanApi
}

return {
blogApi,
kernelApi,
isStorageViaSiyuanApi,
}
}
3 changes: 0 additions & 3 deletions composables/useSiyuanDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const useSiyuanDevice = () => {

const isInSiyuanMainWin = () => {
const deviceType = DeviceDetection.getDevice()
// 三种情况,主窗口、挂件、新窗口
const isSiyuanOrSiyuanNewWin = deviceType === DeviceTypeEnum.DeviceType_Siyuan_MainWin
logger.debug("deviceType=>", deviceType)
logger.debug("isSiyuanOrSiyuanNewWin=>", String(isSiyuanOrSiyuanNewWin))
Expand All @@ -43,7 +42,6 @@ export const useSiyuanDevice = () => {

const isInSiyuanWidget = () => {
const deviceType = DeviceDetection.getDevice()
// 三种情况,主窗口、挂件、新窗口
const isSiyuanOrSiyuanNewWin = deviceType === DeviceTypeEnum.DeviceType_Siyuan_Widget
logger.debug("deviceType=>", deviceType)
logger.debug("isSiyuanOrSiyuanNewWin=>", String(isSiyuanOrSiyuanNewWin))
Expand All @@ -52,7 +50,6 @@ export const useSiyuanDevice = () => {

const isInSiyuanOrSiyuanNewWin = () => {
const deviceType = DeviceDetection.getDevice()
// 三种情况,主窗口、挂件、新窗口
const isSiyuanOrSiyuanNewWin =
deviceType === DeviceTypeEnum.DeviceType_Siyuan_MainWin ||
deviceType === DeviceTypeEnum.DeviceType_Siyuan_NewWin ||
Expand Down
5 changes: 5 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ docker push terwer/nuxt3-blog:latest
docker compose -f docker-compose-hub.yml up --build --remove-orphans
```

```
Error response from daemon: network 84c2ef99951fa08c37c3bf3f6b484bf149bfb283142a306ba285af4053961eaa not found
--force-recreate
```

搭配思源:https://github.com/terwer/my-note-docker/blob/main/docker-compose.yml

直接使用已打包的,参考 `docker-compose-hub.yml`,可能需要配置 `.env.docker`,可以复制 `.env.docker.example`,然后修改即可
Expand Down
3 changes: 3 additions & 0 deletions locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,7 @@ export default {
"share.link.expires.error":
"Please enter a number that must be legal and no older than 7 days! For perpetual effect, enter 0",
"go.home": "Go home",
"form.select": "Select",
"form.nodata": "No data",
"change.ip.title": "Change IP",
}
3 changes: 3 additions & 0 deletions locales/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,7 @@ export default {
"blog.index.home.exists": "对不起,该页面已设置为主页,请先移除主页再取消分享",
"share.link.expires.error": "请输入必须为一个合法且不大于 7 天的数字!如需永久生效,请输入0",
"go.home": "打开主页",
"form.select": "请选择",
"form.nodata": "暂无数据",
"change.ip.title": "切换IP",
}
35 changes: 28 additions & 7 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const generateDynamicV = () => {
}

const isDev = process.env.NODE_ENV === "development"
const appBase = "/"
const appBase = "/plugins/siyuan-blog/"
const staticV = generateDynamicV()

// https://nuxt.com/docs/api/configuration/nuxt-config
Expand All @@ -24,7 +24,14 @@ export default defineNuxtConfig({
},

// build modules
modules: ["@vueuse/nuxt", "@nuxtjs/i18n", "@element-plus/nuxt", "@nuxtjs/color-mode", "@pinia/nuxt", "@nuxt/image"],
modules: [
"@vueuse/nuxt",
"@nuxtjs/i18n-edge",
"@element-plus/nuxt",
"@nuxtjs/color-mode",
"@pinia/nuxt",
"@nuxt/image",
],

// vueuse
vueuse: {
Expand All @@ -47,7 +54,7 @@ export default defineNuxtConfig({
define: {
"process.env.DEV_MODE": `"${isDev}"`,
"process.env.APP_BASE": `"${appBase}"`,
"process.env.SSR": `"true"`,
"process.env.SSR": `"false"`,
},
plugins: [],
},
Expand All @@ -58,6 +65,14 @@ export default defineNuxtConfig({
themes: ["dark"],
},

// https://nuxt.com/docs/guide/going-further/custom-routing#hash-mode-spa
ssr: false,
router: {
options: {
hashMode: true,
},
},

css: ["~/assets/siyuan/style.styl", "~/assets/siyuan/index.styl"],

app: {
Expand Down Expand Up @@ -94,11 +109,17 @@ export default defineNuxtConfig({

// 环境变量
runtimeConfig: {
siyuanAuthToken: process.env.NUXT_SIYUAN_AUTH_TOKEN,
// siyuanAuthToken: process.env.NUXT_SIYUAN_AUTH_TOKEN,
siyuanAuthToken: "",
// siyuanCookie: process.env.NUXT_SIYUAN_COOKIE,
siyuanCookie: "",
public: {
defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE ?? "siyuan",
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL ?? "http://127.0.0.1:6806",
waitTime: process.env.NUXT_PUBLIC_WAIT_TIME,
// defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE,
defaultType: "siyuan",
// siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL,
siyuanApiUrl: "",
// waitTime: process.env.NUXT_PUBLIC_WAIT_TIME,
waitTime: "0",
},
},
})
1 change: 1 addition & 0 deletions nuxt.node.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export default defineNuxtConfig({
// 环境变量
runtimeConfig: {
siyuanAuthToken: process.env.NUXT_SIYUAN_AUTH_TOKEN,
siyuanCookie: process.env.NUXT_SIYUAN_COOKIE,
public: {
defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE ?? "siyuan",
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL ?? "http://127.0.0.1:6806",
Expand Down
2 changes: 2 additions & 0 deletions nuxt.siyuan.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export default defineNuxtConfig({
runtimeConfig: {
// siyuanAuthToken: process.env.NUXT_SIYUAN_AUTH_TOKEN,
siyuanAuthToken: "",
// siyuanCookie: process.env.NUXT_SIYUAN_COOKIE,
siyuanCookie: "",
public: {
// defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE,
defaultType: "siyuan",
Expand Down
1 change: 1 addition & 0 deletions nuxt.vercel.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export default defineNuxtConfig({
// 环境变量
runtimeConfig: {
siyuanAuthToken: process.env.NUXT_SIYUAN_AUTH_TOKEN,
siyuanCookie: process.env.NUXT_SIYUAN_COOKIE,
public: {
defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE,
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL,
Expand Down
33 changes: 33 additions & 0 deletions pages/share.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { JsonUtil, StrUtil } from "zhi-common"
import { useMethodAsync } from "~/composables/useMethodAsync"
import { useMethod } from "~/composables/useMethod"
import { sendMessageToParent } from "~/utils/innerIframeEvent"
import { getAllIps } from "~/utils/urlUtil"
const logger = createAppLogger("share-page")
const { t } = useI18n()
Expand Down Expand Up @@ -40,6 +41,12 @@ const seoMeta = {
} as any
useSeoMeta(seoMeta)
// 初始化 ip
const url = new URL(origin.value)
const hostname = url.hostname
const ips = getAllIps()
ips.push(hostname)
// datas
const attrs = JsonUtil.safeParse<any>(post?.attrs ?? "{}", {})
const formData = reactive({
Expand All @@ -48,6 +55,10 @@ const formData = reactive({
optionEnabled: false,
expiredTime: attrs["custom-expires"] ?? "0",
isHome: setting.homePageId === id.value,
ip: hostname,
ipList: ips.map((ip: string) => {
return { value: ip, label: ip }
}),
})
const { optionState, optionToggle } = useShareOptionToggle(formData.optionEnabled)
Expand Down Expand Up @@ -135,6 +146,12 @@ const handleExpiresTime = async () => {
}
)
}
const handleIpChange = (val: string) => {
const url = new URL(formData.shareLink)
url.hostname = val
formData.shareLink = url.toString()
}
</script>

<template>
Expand Down Expand Up @@ -195,6 +212,20 @@ const handleExpiresTime = async () => {
<div class="item-right"></div>
</div>

<div v-if="optionState" class="share-item">
<div class="item-left">
<span class="change-ip-title">{{ t("change.ip.title") }}</span>
<el-select
v-model="formData.ip"
class="m-2"
:placeholder="t('form.select')"
no-data-text="t('form.nodata')"
@change="handleIpChange"
>
<el-option v-for="item in formData.ipList" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</div>
</div>
<div v-if="optionState" class="share-item expires-link-item">
<div class="expires-link expires-link-label">
{{ t("share.other.option.link.expires") }}
Expand Down Expand Up @@ -302,4 +333,6 @@ const handleExpiresTime = async () => {
.el-page-header__content
::v-deep(.el-switch)
display inline-flex
.change-ip-title
margin-right 10px
</style>
2 changes: 0 additions & 2 deletions plugins/renderer/useServerAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export const useServerAssets = () => {
* @returns {string} 第一个<img>元素的src属性值,如果不存在则返回空字符串
*/
const getFirstImageSrc = (html: string): string => {
logger.info(`getFirstImageSrc called with argument ${html}`) // 记录日志

// 使用 cheerio 加载 HTML 代码
const $ = cheerio.load(html)

Expand Down
1 change: 0 additions & 1 deletion public/resources/appearance/themes/Zhihu/theme.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions siyuan/iframeEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import { createAppLogger } from "~/common/appLogger"
import SiyuanBlog from "~/siyuan/index"

const logger = createAppLogger("iframe-events")
let added = false
const adjustIframeHeight = (iframeId: string, customHeight?: number) => {
const adjustIframeHeight = (pluginInstance: SiyuanBlog, iframeId: string, customHeight?: number) => {
const iframe = document.getElementById(iframeId) as HTMLIFrameElement
let counter = 0
let lastHeight = "0px" // 将初始高度设为 "0px"
Expand All @@ -41,9 +40,9 @@ const adjustIframeHeight = (iframeId: string, customHeight?: number) => {
const iframeBody = iframe?.contentWindow?.document.getElementById("__nuxt") as HTMLElement
let height = `${customHeight ?? iframeBody?.scrollHeight ?? defaultHeight}px`
if (height === lastHeight) {
if (!added) {
if (!pluginInstance.isAddHeight) {
height = height + 10
added = true
pluginInstance.isAddHeight = true
logger.info(`Added 10px height for ${iframeId}`)
}
counter++
Expand Down Expand Up @@ -85,7 +84,10 @@ export const registerIframeEvent = (pluginInstance: SiyuanBlog) => {
// 判断消息类型
if (data.type === "updateHeight") {
logger.info(`Try to cancel loading`)
pluginInstance.popView.cancelLoading()
if (!pluginInstance.popViewLoaded) {
pluginInstance.popView.cancelLoading()
pluginInstance.popViewLoaded = true
}

logger.info(`Received update height message from iframe`)
const height = data.height
Expand All @@ -94,7 +96,7 @@ export const registerIframeEvent = (pluginInstance: SiyuanBlog) => {
iframe.height = `${height}px`
logger.info(`Updated iframe height to ${height}px`)
} else {
adjustIframeHeight(popContentIframeId)
adjustIframeHeight(pluginInstance, popContentIframeId)
logger.info(`Auto adjust iframe height to ${height}px`)
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions siyuan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export default class SiyuanBlog extends Plugin {
public isMobile
public logger
public popView: any
public popViewLoaded = false
public isAddHeight = false

constructor(options: { app: App; id: string; name: string; i18n: IObject }) {
super(options)
Expand Down
Loading

1 comment on commit fd5eace

@vercel
Copy link

@vercel vercel bot commented on fd5eace Jun 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.