Skip to content

Commit

Permalink
添加 markmap 及字数统计自定义
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiflydream committed Dec 29, 2020
1 parent 272befb commit e53a9b6
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 50 deletions.
24 changes: 23 additions & 1 deletion src/common/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,34 @@ let popupItems = [
title: '获得目录列表',
handlerType: 'get_books',
icon: 'el-icon-orange',
},
{
title: '转成思维导图',
handlerType: 'markmap',
icon: 'el-icon-milk-tea',
},
{
title: '配置',
handlerType: 'setting',
icon: 'el-icon-setting',
},
]

/**
* 默认配置
*/
let defaultYuqeuOption = {
fonts: '',
countPrefix: '已经写了',
countSuffix: '个字',
readWordCount: 300,
countCoefficient: 1,
}

export {
mood,
colorBlockItems,
colorHeaderItems,
popupItems
popupItems,
defaultYuqeuOption
}
34 changes: 19 additions & 15 deletions src/content_scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
} = require("bisheng-formatter-core");

const { removeAllBlank } = require("../common/utils.js")
import { defaultYuqeuOption } from '../common/config.js';


const config = {
Expand Down Expand Up @@ -70,7 +71,7 @@ document.addEventListener('DOMContentLoaded', function () {
let currentTabUrl = window.location.href;
if (currentTabUrl.includes('yuque')) {
// 字体替换
chrome.storage.local.get('yuqueOption', (res) => {
chrome.storage.sync.get({yuqueOption: defaultYuqeuOption}, (res) => {
let option = res.yuqueOption;
if (option.fonts) {
// 当前页面设置字体
Expand All @@ -90,31 +91,38 @@ document.addEventListener('DOMContentLoaded', function () {
}
})

chrome.storage.sync.get({yuqueOption: defaultYuqeuOption}, (res) => {
let option = res.yuqueOption;
// 统计编辑面文字数
if (currentTabUrl.endsWith('edit')) {

setInterval(() => {
let spanCount = $(".lark-editor-save-tip").children('span').length;
let totlaCount = removeAllBlank($(".lake-content-editor-core").text()).length;
let totalWordCount = getWordCount(totlaCount, '.lake-content-editor-core');
if (spanCount <= 1) {
$(".lark-editor-save-tip").append(`<span>&nbsp;(已经写了${totalWordCount}个字)</span>`)
$(".lark-editor-save-tip").append(`<span>&nbsp;(${option.countPrefix} ${(totalWordCount * option.countCoefficient).toFixed(1) / 1} ${option.countSuffix})</span>`)
} else {
$(".lark-editor-save-tip").children('span').last().text(` (已经写了${totalWordCount}个字)`);
$(".lark-editor-save-tip").children('span').last().text(` (${option.countPrefix} ${(totalWordCount * option.countCoefficient).toFixed(1) / 1} ${option.countSuffix})`);
}
}, 500);

} else {

// 统计阅读时间
// 这里不能使用,setTimeout,因为通过目录点击,不会重新加载页面
setInterval(() => {
let spanCount = $("#header > div > div.header-crumb > span").children('span').length;
let wordCount = getWordCount(removeAllBlank($("#content").text()).length, '#content');
if (spanCount <= 1) {
$("#header > div > div.header-crumb > span").last().append(`<span>&nbsp;(需阅读 ${Math.ceil(wordCount / 300)} 分钟)</span>`)
$("#header > div > div.header-crumb > span").last().append(`<span>&nbsp;(需阅读 ${Math.ceil(wordCount / option.readWordCount)} 分钟)</span>`)
} else {
$("#header > div > div.header-crumb > span").children('span').last().text(` (需阅读 ${Math.ceil(wordCount / 300)} 分钟)`);
$("#header > div > div.header-crumb > span").children('span').last().text(` (需阅读 ${Math.ceil(wordCount / option.readWordCount)} 分钟)`);
}
}, 1000);

}
});
}
});

Expand Down Expand Up @@ -157,16 +165,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log(request.cmd);
switch (request.cmd) {
case 'toc':
let headMk = '';
// $(".yuque-doc-content :header").each(function () {
// let tagName = $(this)[0].tagName;
// let title = $(this).text();
// let id = $(this).attr('id');
// let head = ' '.repeat(tagName[1]) + '-';
// if (!/^\s*$/.test(title)) {
// headMk += `${head} (${title})[${id}]` + '<br/>';
// }
// });
let headMk = '';
$(".lake-content-editor-core :header").each(function () {
let tagName = $(this)[0].tagName;
let title = $(this).text();
Expand Down Expand Up @@ -222,6 +221,11 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
});
sendResponse(urls)
break;
// 获得 markmap 文本
case 'get_markmap':
let markdownText = $('pre').text();
sendResponse(markdownText);
break;
default:
break;
}
Expand Down
25 changes: 25 additions & 0 deletions src/markmap/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<div class="markmap">
{{ message }}
</div>
</template>

<script>
export default {
data() {
return {
message: '无数据',
};
},
created() {
chrome.storage.local.get({temp: '获取数据失败'}, (res) => {
if (res.temp) {
this.message = res.temp;
}
});
},
};
</script>



2 changes: 2 additions & 0 deletions src/markmap/[email protected]

Large diffs are not rendered by default.

122 changes: 122 additions & 0 deletions src/markmap/markmap-autoloader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*! markmap-autoloader v0.1.1 | MIT License */ ! function () {
"use strict";

function t() {
return (t = Object.assign || function (t) {
for (var n = 1; n < arguments.length; n++) {
var r = arguments[n];
for (var e in r) Object.prototype.hasOwnProperty.call(r, e) && (t[e] = r[e])
}
return t
}).apply(this, arguments)
}
/*! markmap-common v0.1.2 | MIT License */
Math.random().toString(36).slice(2, 8);

function n(t, n, r) {
const e = document.createElement(t);
return n && Object.entries(n).forEach((([t, n]) => {
e[t] = n
})), r && Object.entries(r).forEach((([t, n]) => {
e.setAttribute(t, n)
})), e
}
const r = function (t) {
const n = {};
return function (...r) {
const e = `${r[0]}`;
let a = n[e];
return a || (a = {
value: t(...r)
}, n[e] = a), a.value
}
}((t => {
document.head.append(n("link", {
rel: "preload",
as: "script",
href: t
}))
}));

function e(r, e) {
if ("script" === r.type) return new Promise(((e, a) => {
var o;
document.head.append(n("script", t({}, r.data, {
onload: e,
onerror: a
}))), null != (o = r.data) && o.src || e()
}));
if ("iife" === r.type) {
const {
fn: t,
getParams: n
} = r.data;
t(...(null == n ? void 0 : n(e)) || [])
}
}
const a = {};

function o(t) {
const {
Transformer: n,
Markmap: r
} = window.markmap, e = t.textContent.split("\n");
let o = 1 / 0;
e.forEach((t => {
const n = t.match(/^\s*/)[0].length;
n < t.length && (o = Math.min(o, n))
}));
const c = e.map((t => t.slice(o))).join("\n"),
s = new n;
t.innerHTML = "<svg></svg>";
const i = t.firstChild,
p = r.create(i),
l = () => {
const t = function (t, n) {
const {
root: r,
features: e
} = t.transform(n), o = Object.keys(e).filter((t => !a[t]));
o.forEach((t => {
a[t] = !0
}));
const {
styles: c,
scripts: s
} = t.getAssets(o), {
loadJS: i,
loadCSS: p
} = window.markmap;
return c && p(c), s && i(s), r
}(s, c);
p.setData(t), p.fit()
};
s.hooks.retransform.tap(l), l()
}!async function () {
await async function (n, a) {
const o = n.filter((t => {
var n;
return "script" === t.type && (null == (n = t.data) ? void 0 : n.src)
}));
o.length > 1 && o.forEach((t => r(t.data.src))), a = t({
getMarkmap: () => window.markmap
}, a);
for (const t of n) await e(t, a)
}([{
type: "script",
data: {
src: "./[email protected]"
}
}, {
type: "script",
data: {
src: "./[email protected]"
}
},{
type: "script",
data: {
src: "./[email protected]"
}
}]), document.querySelectorAll(".markmap").forEach(o)
}()
}();
8 changes: 8 additions & 0 deletions src/markmap/[email protected]

Large diffs are not rendered by default.

Loading

0 comments on commit e53a9b6

Please sign in to comment.