Skip to content

Commit

Permalink
fix: #342 适配2.6.3+的localstorage改动,重构配置数据存储方案-Eletron环境使用JsonLocalStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jan 30, 2023
1 parent c55437e commit 9f05f68
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 19 deletions.
133 changes: 131 additions & 2 deletions public/lib/json-localstorage/json-localstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,134 @@

// 警告⚠️:请勿在非思源笔记浏览器环境调用此文件中的任何方法

// JsonLocalStorage
const initJsonLocalStorage = () => {}
"use strict"
var fs = require("fs")
var path = require("path")

// 警告⚠️:请勿在非Electron环境调用此文件中的任何方法

var LocalStorage
;(function (LocalStorage) {
LocalStorage.filepath = ""
LocalStorage.filename = ".storage.json"

LocalStorage.length = getLength()

function init(filepath) {
LocalStorage.filepath = filepath

window.JsonLocalStorage = LocalStorage
console.log("挂载window.JsonLocalStorage", window.JsonLocalStorage)

const fullpath = path.join(__dirname, LocalStorage.filepath)
console.log("设置json配置目录", fullpath)
}

LocalStorage.init = init

function switchCfg(filename) {
LocalStorage.filename = filename
}

LocalStorage.switchCfg = switchCfg

function getItem(key) {
return getStoredItems()[key] || null
}

LocalStorage.getItem = getItem

function setItem(key, value) {
var obj = getStoredItems()
obj[key] = value
LocalStorage.length = countKeys(obj)
writeFile(obj)
}

LocalStorage.setItem = setItem

function removeItem(key) {
var obj = getStoredItems()
delete obj[key]
LocalStorage.length = countKeys(obj)
writeFile(obj)
}

LocalStorage.removeItem = removeItem

function clear() {
LocalStorage.length = 0
writeFile({})
return undefined
}

LocalStorage.clear = clear

function getLength() {
return countKeys(getStoredItems())
}

LocalStorage.getLength = getLength

function countKeys(obj) {
var size = 0
for (var key in obj) {
if (obj.hasOwnProperty(key)) size++
}
return size
}

function key(n) {
var obj = getStoredItems()
var counter = 0
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (n == counter) {
return key
} else if (counter > n) {
return null
}
counter++
}
}
return null
}

LocalStorage.key = key

function getStoredItems() {
try {
return JSON.parse(readFile())
} catch (e) {
return {}
}
}

function readFile() {
return fs.readFileSync(getFilepath(), getFileOptions())
}

function writeFile(obj) {
return fs.writeFileSync(
getFilepath(),
JSON.stringify(obj),
getFileOptions()
)
}

function getFilepath() {
const fullpath = path.join(__dirname, LocalStorage.filepath)
if (!fs.existsSync(fullpath)) {
fs.mkdirSync(fullpath)
}
// console.log("fullpath=>", fullpath)
// console.log("LocalStorage.filename=>", LocalStorage.filename)
return path.join(fullpath, LocalStorage.filename)
}

function getFileOptions() {
return { encoding: "utf8" }
}
})(LocalStorage || (LocalStorage = {}))
module.exports = LocalStorage
console.log("json-localstorage register success")
59 changes: 42 additions & 17 deletions public/lib/siyuanhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,41 @@ const init = () => {
return
}

if (!isSiyuanNewWin()) {
// 初始化插槽
if (isSiyuanWidget()) {
const slotLibPath = `${parent.window.siyuan.config.system.dataDir}/widgets/sy-post-publisher/lib/siyuan/silot.js`
console.log("iframe挂件将要从以下位置引入插槽", slotLibPath)
const initSlot = parent.window.require(slotLibPath)
initSlot()
// 初始化插槽
if (isSiyuanWidget()) {
const slotLibPath = `${parent.window.siyuan.config.system.dataDir}/widgets/sy-post-publisher/lib/siyuan/silot.js`
console.log("iframe挂件将要从以下位置引入插槽", slotLibPath)
const initSlot = parent.window.require(slotLibPath)
initSlot()

// 初始化发布辅助功能
const publishHelperLibPath = `${parent.window.siyuan.config.system.dataDir}/widgets/sy-post-publisher/lib/siyuan/publish-helper.js`
// 初始化发布辅助功能
const publishHelperLibPath = `${parent.window.siyuan.config.system.dataDir}/widgets/sy-post-publisher/lib/siyuan/publish-helper.js`
console.log(
"iframe挂件将要从以下位置引入发布辅助功能",
publishHelperLibPath
)
const initPublishHelper = parent.window.require(publishHelperLibPath)
initPublishHelper()

// 挂载JsonLocalStorage到window
const jsonLocalstorageLibPath = `${parent.window.siyuan.config.system.dataDir}/widgets/sy-post-publisher/lib/json-localstorage/json-localstorage.js`
console.log(
"iframe挂件将要从以下位置引入json-localstorage",
jsonLocalstorageLibPath
)
const LocalStorage = parent.window.require(jsonLocalstorageLibPath)
LocalStorage.init("../../../../storage/syp/")
} else {
if (isSiyuanNewWin()) {
// 挂载JsonLocalStorage到window
const jsonLocalstorageLibPath = `${window.terwer.dataDir}/widgets/sy-post-publisher/lib/json-localstorage/json-localstorage.js`
console.log(
"iframe挂件将要从以下位置引入发布辅助功能",
publishHelperLibPath
"自定义js片段将要从以下位置引入json-localstorage",
jsonLocalstorageLibPath
)
const initPublishHelper = parent.window.require(publishHelperLibPath)
initPublishHelper()
const LocalStorage = window.require(jsonLocalstorageLibPath)
// 设置json配置目录
LocalStorage.init("../../../../storage/syp/")
} else {
const slotLibPath = `${window.siyuan.config.system.dataDir}/widgets/sy-post-publisher/lib/siyuan/silot.js`
console.log("自定义js片段将要从以下位置引入插槽", slotLibPath)
Expand All @@ -88,12 +107,18 @@ const init = () => {
)
const initPublishHelper = window.require(publishHelperLibPath)
initPublishHelper()

// 挂载JsonLocalStorage到window
const jsonLocalstorageLibPath = `${parent.window.siyuan.config.system.dataDir}/widgets/sy-post-publisher/lib/json-localstorage/json-localstorage.js`
console.log(
"自定义js片段将要从以下位置引入json-localstorage",
jsonLocalstorageLibPath
)
const LocalStorage = window.require(jsonLocalstorageLibPath)
// 设置json配置目录
LocalStorage.init("../../../../storage/syp/")
}
}

// 挂载JsonLocalStorage到window
// const jsonLocalstorageLibPath = `${parent.window.siyuan.config.system.dataDir}/widgets/sy-hello/lib/json-localstorage/json-localstorage.js`
// console.log("将要从以下位置引入json-localstorage", jsonLocalstorageLibPath)
}

// 统一的初始化入口
Expand Down

0 comments on commit 9f05f68

Please sign in to comment.