Skip to content

Commit

Permalink
Merge pull request #155 from obgnail/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
obgnail authored Sep 9, 2023
2 parents 008d1c1 + a9624af commit 30a19b0
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 150 deletions.
139 changes: 73 additions & 66 deletions plugin/auto_number.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,58 +186,6 @@ class autoNumberPlugin extends global._basePlugin {
}

init = () => {
this.decoMixin = {
inExport: false,

beforeExport: (...args) => {
this.inExport = true;
args[0].extraCss = `body {font-variant-ligatures: no-common-ligatures;} ` + this.getStyleString();
},

afterGetHeaderMatrix: headers => {
if (!this.inExport) return;
this.inExport = false;

const pValue = {H2: 0, H3: 0, H4: 0, H5: 0, H6: 0};
headers.forEach(header => {
const tagName = "H" + header[0];
if (!pValue.hasOwnProperty(tagName)) return;

let numbering = "";
switch (tagName) {
case "H1":
pValue.H2 = 0;
break
case "H2":
pValue.H3 = 0;
pValue.H2++;
numbering = `${pValue.H2}. `;
break
case "H3":
pValue.H4 = 0;
pValue.H3++;
numbering = `${pValue.H2}.${pValue.H3} `;
break
case "H4":
pValue.H5 = 0;
pValue.H4++;
numbering = `${pValue.H2}.${pValue.H3}.${pValue.H4} `;
break
case "H5":
pValue.H6 = 0;
pValue.H5++;
numbering = `${pValue.H2}.${pValue.H3}.${pValue.H4}.${pValue.H5} `;
break
case "H6":
pValue.H6++;
numbering = `${pValue.H2}.${pValue.H3}.${pValue.H4}.${pValue.H5}.${pValue.H6} `;
break
}
header[1] = numbering + header[1];
})
}
}

this.callArgs = [
{
arg_name: "禁用/启用大纲自动编号",
Expand Down Expand Up @@ -279,21 +227,8 @@ class autoNumberPlugin extends global._basePlugin {

process = () => {
this.init();

if (this.config.ENABLE_WHEN_EXPORT) {
this.utils.decorate(
() => (File && File.editor && File.editor.export && File.editor.export.exportToHTML),
"File.editor.export.exportToHTML",
this.decoMixin.beforeExport,
null
);
this.utils.decorate(
() => (File && File.editor && File.editor.library && File.editor.library.outline
&& File.editor.library.outline.getHeaderMatrix),
"File.editor.library.outline.getHeaderMatrix",
null,
this.decoMixin.afterGetHeaderMatrix
);
new exportPDFHelper(this).process();
}
}

Expand Down Expand Up @@ -341,6 +276,78 @@ class autoNumberPlugin extends global._basePlugin {
}
}

// 解决导出pdf时目录没有编号的问题
class exportPDFHelper {
constructor(controller) {
this.inExport = false;
this.controller = controller
}

beforeExport = (...args) => {
this.inExport = true;
args[0].extraCss = `body {font-variant-ligatures: no-common-ligatures;} ` + this.controller.getStyleString();
}

afterGetHeaderMatrix = headers => {
if (!this.inExport) return;
this.inExport = false;

const pValue = {H2: 0, H3: 0, H4: 0, H5: 0, H6: 0};
headers.forEach(header => {
const tagName = "H" + header[0];
if (!pValue.hasOwnProperty(tagName)) return;

let numbering = "";
switch (tagName) {
case "H1":
pValue.H2 = 0;
break
case "H2":
pValue.H3 = 0;
pValue.H2++;
numbering = `${pValue.H2}. `;
break
case "H3":
pValue.H4 = 0;
pValue.H3++;
numbering = `${pValue.H2}.${pValue.H3} `;
break
case "H4":
pValue.H5 = 0;
pValue.H4++;
numbering = `${pValue.H2}.${pValue.H3}.${pValue.H4} `;
break
case "H5":
pValue.H6 = 0;
pValue.H5++;
numbering = `${pValue.H2}.${pValue.H3}.${pValue.H4}.${pValue.H5} `;
break
case "H6":
pValue.H6++;
numbering = `${pValue.H2}.${pValue.H3}.${pValue.H4}.${pValue.H5}.${pValue.H6} `;
break
}
header[1] = numbering + header[1];
})
}

process = () => {
this.controller.utils.decorate(
() => (File && File.editor && File.editor.export && File.editor.export.exportToHTML),
"File.editor.export.exportToHTML",
this.beforeExport,
null
);
this.controller.utils.decorate(
() => (File && File.editor && File.editor.library && File.editor.library.outline
&& File.editor.library.outline.getHeaderMatrix),
"File.editor.library.outline.getHeaderMatrix",
null,
this.afterGetHeaderMatrix
);
}
}

module.exports = {
plugin: autoNumberPlugin
};
14 changes: 8 additions & 6 deletions plugin/custom/plugins/kanban.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ class kanbanPlugin extends BaseCustomPlugin {
}
)

const that = this;
$("#write").on("wheel", ".plugin-kanban-content", function (ev) {
if (that.utils.metaKeyPressed(ev.originalEvent)) {
this.scrollLeft += ev.originalEvent.deltaY;
}
})
if (this.config.CTRL_WHEEL_TO_SCROLL) {
const that = this;
$("#write").on("wheel", ".plugin-kanban-content", function (ev) {
if (that.utils.metaKeyPressed(ev.originalEvent)) {
this.scrollLeft += ev.originalEvent.deltaY;
}
})
}
}

callback = anchorNode => {
Expand Down
119 changes: 53 additions & 66 deletions plugin/export_enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,70 @@ class exportEnhancePlugin extends global._basePlugin {
init = () => {
this.Path = this.utils.Package.Path;
this.tempFolder = this.utils.tempFolder; // i‘d like to shit here
this.regexp = new RegExp(`<img.*?src="(.*?)".*?>`, "gs");
}

this.decoMixin = {
regexp: new RegExp(`<img.*?src="(.*?)".*?>`, "gs"),
writeIdx: -1,
imageMap: {}, // map src to localFileName, use for network image only

init: () => {
this.decoMixin.writeIdx = -1
this.decoMixin.imageMap = {}
},
process = () => {
this.init();
this.utils.decorate(
() => (File && File.editor && File.editor.export && File.editor.export.exportToHTML),
"File.editor.export.exportToHTML",
null,
this.afterExportToHtml,
true,
);
}

downloadAllImage: async (html) => {
for (let result of html.matchAll(this.decoMixin.regexp)) {
if (result.length !== 2 || result.index < this.decoMixin.writeIdx || !this.utils.isNetworkImage(result[1])) continue
const src = result[1];
if (!this.decoMixin.imageMap.hasOwnProperty(src)) { // single flight
const filename = Math.random() + "_" + this.Path.basename(src);
const {state} = await JSBridge.invoke("app.download", src, this.tempFolder, filename);
if (state === "completed") {
this.decoMixin.imageMap[src] = filename;
}
}
}
},
afterExportToHtml = async (exportResult, ...args) => {
if (!this.config.ENABLE) return exportResult;
const exportConfig = args[0];
if (!exportConfig || exportConfig["type"] !== "html" && exportConfig["type"] !== "html-plain") return exportResult;

afterExportToHtml: async (exportResult, ...args) => {
if (!this.config.ENABLE) return exportResult;
const html = await exportResult;
const writeIdx = html.indexOf(`id='write'`);
if (writeIdx === -1) return this.simplePromise(html);

this.decoMixin.init();
const imageMap = (this.config.DOWNLOAD_NETWORK_IMAGE) ? await this.downloadAllImage(html, writeIdx) : {};

const exportConfig = args[0];
if (!exportConfig || exportConfig["type"] !== "html" && exportConfig["type"] !== "html-plain") return exportResult;
const dirname = this.getCurDir();
const newHtml = html.replace(this.regexp, (origin, src, srcIdx) => {
if (srcIdx < writeIdx) return origin;

const html = await exportResult;
this.decoMixin.writeIdx = html.indexOf(`id='write'`);
if (this.decoMixin.writeIdx === -1) return this.simplePromise(html);
let result = origin;
let imagePath;
try {
if (this.utils.isNetworkImage(src)) {
if (!this.config.DOWNLOAD_NETWORK_IMAGE || !imageMap.hasOwnProperty(src)) return origin

if (this.config.DOWNLOAD_NETWORK_IMAGE) {
await this.decoMixin.downloadAllImage(html);
const path = imageMap[src];
imagePath = this.Path.join(this.tempFolder, path);
} else {
imagePath = this.Path.join(dirname, src);
}

const dirname = this.getCurDir();
const newHtml = html.replace(this.decoMixin.regexp, (origin, src, srcIdx) => {
if (srcIdx < this.decoMixin.writeIdx) return origin;

let result = origin;
let imagePath;
try {
if (this.utils.isNetworkImage(src)) {
if (!this.config.DOWNLOAD_NETWORK_IMAGE || !this.decoMixin.imageMap.hasOwnProperty(src)) return origin
const path = this.decoMixin.imageMap[src];
imagePath = this.Path.join(this.tempFolder, path);
} else {
imagePath = this.Path.join(dirname, src);
}
const base64Data = this.toBase64(imagePath);
result = origin.replace(src, base64Data);
} catch (e) {
console.error("export error:", e);
}
return result;
})
return this.simplePromise(newHtml);
const base64Data = this.toBase64(imagePath);
result = origin.replace(src, base64Data);
} catch (e) {
console.error("export error:", e);
}
}
return result;
})
return this.simplePromise(newHtml);
}

process = () => {
this.init();

this.utils.decorate(
() => (File && File.editor && File.editor.export && File.editor.export.exportToHTML),
"File.editor.export.exportToHTML",
null,
this.decoMixin.afterExportToHtml,
true,
);
downloadAllImage = async (html, writeIdx) => {
const imageMap = {} // map src to localFileName, use for network image only
for (let result of html.matchAll(this.regexp)) {
if (result.length !== 2 || result.index < writeIdx || !this.utils.isNetworkImage(result[1])) continue
const src = result[1];
if (!imageMap.hasOwnProperty(src)) { // single flight
const filename = Math.random() + "_" + this.Path.basename(src);
const {state} = await JSBridge.invoke("app.download", src, this.tempFolder, filename);
if (state === "completed") {
imageMap[src] = filename;
}
}
}
return this.simplePromise(imageMap)
}

getCurDir = () => {
Expand Down
6 changes: 4 additions & 2 deletions plugin/global/settings/custom_plugin.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ enable = true
HIDE_DESC_WHEN_EMPTY = true
# 看板最大高度(若<0,则全部显示)
KANBAN_MAX_HEIGHT = 700
# 当鼠标位于看板时,ctrl+wheel滚动看板
CTRL_WHEEL_TO_SCROLL = true
# 看板的颜色
# 如果你新增了颜色,请保持 KANBAN_COLOR 和 TASK_COLOR 数量一致,否则颜色会乱套,就不好看了
KANBAN_COLOR = [
Expand Down Expand Up @@ -43,8 +45,8 @@ TEMPLATE = """```kanban
- 当描述为空时隐藏描述框
## In-Progress
- 当任务数量太多,出现滚动条时(可以将鼠标置于看板下,使用【鼠标滚轮】切换任务)
- 当看板数量太多,出现滚动条时(可以将鼠标置于看板下,使用【ctrl+鼠标滚轮】切换看板)
- 当任务数量太多,出现滚动条时(可以将鼠标置于看板下,使用【鼠标滚轮】滚动任务)
- 当看板数量太多,出现滚动条时(可以将鼠标置于看板下,使用【ctrl+鼠标滚轮】滚动看板)
## Completed
- NOTE(语法是开发者定义的,没有通用性,只建议用于【每日任务】临时使用)
Expand Down
2 changes: 2 additions & 0 deletions plugin/global/settings/settings.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ CLOSE_HOTKEY = ["ctrl+w"]
SWITCH_NEXT_TAB_HOTKEY = ["ctrl+PageDown", "ctrl+Tab"]
# 切换上一个标签页的快捷键
SWITCH_PREVIOUS_TAB_HOTKEY = ["ctrl+PageUp", "ctrl+shift+Tab"]
# 当鼠标位于标签页时,ctrl+wheel切换标签页
CTRL_WHEEL_TO_SCROLL = true
# 内部使用
LOOP_DETECT_INTERVAL = 35

Expand Down
21 changes: 11 additions & 10 deletions plugin/window_tab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,18 @@ class windowTabBarPlugin extends global._basePlugin {
}
})

if (this.config.CTRL_WHEEL_TO_SCROLL) {
this.entities.tabBar.addEventListener("wheel", ev => {
const target = ev.target.closest("#plugin-window-tab .tab-bar");
if (!target) return;

this.entities.tabBar.addEventListener("wheel", ev => {
const target = ev.target.closest("#plugin-window-tab .tab-bar");
if (!target) return;

if (this.utils.metaKeyPressed(ev)) {
(ev.deltaY < 0) ? this.previousTab() : this.nextTab();
} else {
target.scrollLeft += ev.deltaY;
}
})
if (this.utils.metaKeyPressed(ev)) {
(ev.deltaY < 0) ? this.previousTab() : this.nextTab();
} else {
target.scrollLeft += ev.deltaY;
}
})
}

this.entities.content.addEventListener("scroll", () => {
if (this.tabUtil.tabs[this.tabUtil.activeIdx]) {
Expand Down

0 comments on commit 30a19b0

Please sign in to comment.