md/src/assets/scripts/util.js

272 lines
8.1 KiB
JavaScript
Raw Normal View History

2020-10-20 11:43:11 +00:00
import default_theme from "./themes/default-theme";
import prettier from "prettier/standalone";
import prettierMarkdown from "prettier/parser-markdown";
2020-12-03 00:51:39 +08:00
import prettierCss from "prettier/parser-postcss";
2020-02-15 21:44:42 +08:00
2020-01-13 22:16:04 +08:00
// 设置自定义颜色
export function setColorWithTemplate(template) {
return function (color) {
2020-10-20 11:43:11 +00:00
let custom_theme = JSON.parse(JSON.stringify(template));
custom_theme.block.h1["border-bottom"] = `2px solid ${color}`;
custom_theme.block.h2["background"] = color;
custom_theme.block.h3["border-left"] = `3px solid ${color}`;
custom_theme.block.h4["color"] = color;
custom_theme.inline.strong["color"] = color;
return custom_theme;
};
}
export const setColorWithCustomTemplate = function setColorWithCustomTemplate(
template,
color
) {
2020-10-20 11:43:11 +00:00
let custom_theme = JSON.parse(JSON.stringify(template));
custom_theme.block.h1["border-bottom"] = `2px solid ${color}`;
custom_theme.block.h2["background"] = color;
custom_theme.block.h3["border-left"] = `3px solid ${color}`;
custom_theme.block.h4["color"] = color;
custom_theme.inline.strong["color"] = color;
return custom_theme;
};
2020-01-13 22:16:04 +08:00
// 设置自定义字体大小
export function setFontSizeWithTemplate(template) {
return function (fontSize) {
2020-10-20 11:43:11 +00:00
let custom_theme = JSON.parse(JSON.stringify(template));
custom_theme.block.h1["font-size"] = `${fontSize * 1.14}px`;
custom_theme.block.h2["font-size"] = `${fontSize * 1.1}px`;
custom_theme.block.h3["font-size"] = `${fontSize}px`;
custom_theme.block.h4["font-size"] = `${fontSize}px`;
return custom_theme;
};
2020-01-13 22:16:04 +08:00
}
2020-10-20 11:43:11 +00:00
export const setColor = setColorWithTemplate(default_theme);
export const setFontSize = setFontSizeWithTemplate(default_theme);
2020-01-13 22:16:04 +08:00
export function customCssWithTemplate(jsonString, color, theme) {
2020-10-20 11:43:11 +00:00
let custom_theme = JSON.parse(JSON.stringify(theme));
// block
2020-10-20 11:43:11 +00:00
custom_theme.block.h1["border-bottom"] = `2px solid ${color}`;
custom_theme.block.h2["background"] = color;
custom_theme.block.h3["border-left"] = `3px solid ${color}`;
custom_theme.block.h4["color"] = color;
custom_theme.inline.strong["color"] = color;
custom_theme.block.h1 = Object.assign(custom_theme.block.h1, jsonString.h1);
custom_theme.block.h2 = Object.assign(custom_theme.block.h2, jsonString.h2);
custom_theme.block.h3 = Object.assign(custom_theme.block.h3, jsonString.h3);
custom_theme.block.h4 = Object.assign(custom_theme.block.h4, jsonString.h4);
custom_theme.block.p = Object.assign(custom_theme.block.p, jsonString.p);
custom_theme.block.blockquote = Object.assign(
custom_theme.block.blockquote,
jsonString.blockquote
2020-10-20 11:43:11 +00:00
);
custom_theme.block.blockquote_p = Object.assign(
custom_theme.block.blockquote_p,
jsonString.blockquote_p
2020-10-20 11:43:11 +00:00
);
custom_theme.block.image = Object.assign(
custom_theme.block.image,
jsonString.image
2020-10-20 11:43:11 +00:00
);
// inline
custom_theme.inline.strong = Object.assign(
custom_theme.inline.strong,
jsonString.strong
2020-10-20 11:43:11 +00:00
);
custom_theme.inline.codespan = Object.assign(
custom_theme.inline.codespan,
jsonString.codespan
2020-10-20 11:43:11 +00:00
);
custom_theme.inline.link = Object.assign(
custom_theme.inline.link,
jsonString.link
2020-10-20 11:43:11 +00:00
);
custom_theme.inline.wx_link = Object.assign(
custom_theme.inline.wx_link,
jsonString.wx_link
2020-10-20 11:43:11 +00:00
);
2020-10-20 11:43:11 +00:00
return custom_theme;
2020-01-13 22:16:04 +08:00
}
/**
* 将CSS形式的字符串转换为JSON
*
* @param {css字符串} css
*/
export function css2json(css) {
// 移除CSS所有注释
let open, close;
while (
2020-10-20 11:43:11 +00:00
(open = css.indexOf("/*")) !== -1 &&
(close = css.indexOf("*/")) !== -1
) {
2020-10-20 11:43:11 +00:00
css = css.substring(0, open) + css.substring(close + 2);
2020-01-13 22:16:04 +08:00
}
// 初始化返回值
2020-10-20 11:43:11 +00:00
let json = {};
2020-10-20 11:43:11 +00:00
while (
css.length > 0 &&
css.indexOf("{") !== -1 &&
css.indexOf("}") !== -1
) {
// 存储第一个左/右花括号的下标
2020-10-20 11:43:11 +00:00
const lbracket = css.indexOf("{");
const rbracket = css.indexOf("}");
// 第一步将声明转换为Object
// `font: 'Times New Roman' 1em; color: #ff0000; margin-top: 1em;`
// ==>
// `{"font": "'Times New Roman' 1em", "color": "#ff0000", "margin-top": "1em"}`
// 辅助方法将array转为object
function toObject(array) {
2020-10-20 11:43:11 +00:00
let ret = {};
array.forEach((e) => {
const index = e.indexOf(":");
const property = e.substring(0, index).trim();
const value = e.substring(index + 1).trim();
ret[property] = value;
});
return ret;
}
// 切割声明块并移除空白符,然后放入数组中
let declarations = css
.substring(lbracket + 1, rbracket)
2020-10-20 11:43:11 +00:00
.split(";")
.map((e) => e.trim())
.filter((e) => e.length > 0); // 移除所有""空值
// 转为Object对象
2020-10-20 11:43:11 +00:00
declarations = toObject(declarations);
// 第二步:选择器处理,每个选择器会与它对应的声明相关联,如:
// `h1, p#bar {color: red}`
// ==>
// {"h1": {color: red}, "p#bar": {color: red}}
let selectors = css
.substring(0, lbracket)
// 以,切割,并移除空格:`"h1, p#bar, span.foo"` => ["h1", "p#bar", "span.foo"]
2020-10-20 11:43:11 +00:00
.split(",")
.map((selector) => selector.trim());
// 迭代赋值
2020-10-20 11:43:11 +00:00
selectors.forEach((selector) => {
// 若不存在,则先初始化
2020-10-20 11:43:11 +00:00
if (!json[selector]) json[selector] = {};
// 赋值到JSON
2020-10-20 11:43:11 +00:00
Object.keys(declarations).forEach((key) => {
json[selector][key] = declarations[key];
});
});
// 继续下个声明块
2020-10-20 11:43:11 +00:00
css = css.slice(rbracket + 1).trim();
}
2020-01-13 22:16:04 +08:00
// 返回JSON形式的结果串
2020-10-20 11:43:11 +00:00
return json;
2020-01-13 22:16:04 +08:00
}
2020-05-01 21:30:25 +08:00
/**
* 将编辑器内容保存到 LocalStorage
2020-10-20 11:43:11 +00:00
* @param {*} editor
* @param {*} name
2020-05-01 21:30:25 +08:00
*/
export function saveEditorContent(editor, name) {
2020-10-20 11:43:11 +00:00
const content = editor.getValue(0);
2020-05-01 21:30:25 +08:00
if (content) {
2020-10-20 11:43:11 +00:00
localStorage.setItem(name, content);
2020-05-01 21:30:25 +08:00
} else {
2020-10-20 11:43:11 +00:00
localStorage.removeItem(name);
2020-05-01 21:30:25 +08:00
}
}
2020-11-21 09:33:45 +08:00
/**
* 格式化文档
2020-11-21 01:34:46 +00:00
* @param {文档内容} content
2020-11-21 09:33:45 +08:00
*/
2020-05-31 17:30:44 +08:00
export function formatDoc(content) {
const doc = prettier.format(content, {
2020-10-20 11:43:11 +00:00
parser: "markdown",
plugins: [prettierMarkdown],
});
return doc;
}
2020-07-04 00:55:40 +08:00
2020-12-03 00:51:39 +08:00
/**
* 格式化css
* @param {css内容}} content
*/
export function formatCss(content) {
const doc = prettier.format(content, {
parser: "css",
plugins: [prettierCss],
});
return doc;
}
2020-10-20 11:43:11 +00:00
export function fixCodeWhiteSpace(value = "pre") {
const preDomList = document.getElementsByClassName("code__pre");
2020-07-04 00:55:40 +08:00
if (preDomList.length > 0) {
2020-10-20 11:43:11 +00:00
preDomList.forEach((pre) => {
2020-07-04 00:55:40 +08:00
pre.style.whiteSpace = value;
2020-10-20 11:43:11 +00:00
});
2020-07-04 00:55:40 +08:00
}
2020-07-13 00:26:29 +08:00
}
2020-11-21 09:33:45 +08:00
/**
* 下载原始 Markdown 文档
2020-11-21 01:34:46 +00:00
* @param {文档内容} doc
2020-11-21 09:33:45 +08:00
*/
2020-11-22 22:36:35 +08:00
export function downloadMD(doc) {
2020-10-20 11:43:11 +00:00
let downLink = document.createElement("a");
2020-07-13 00:26:29 +08:00
2020-10-20 11:43:11 +00:00
downLink.download = "content.md";
downLink.style.display = "none";
2020-07-13 00:26:29 +08:00
let blob = new Blob([doc]);
downLink.href = URL.createObjectURL(blob);
document.body.appendChild(downLink);
downLink.click();
document.body.removeChild(downLink);
2020-08-31 20:48:22 +08:00
}
2020-10-13 00:07:14 +08:00
/**
* 生成列表字符串
* @param {*} data 对应内容集合
* @param {*} rows
* @param {*} cols
*/
2020-10-20 11:43:11 +00:00
export function createTable({ data, rows, cols }) {
2020-10-13 00:07:14 +08:00
let table = "";
let currRow = [];
for (let i = 0; i < rows + 2; ++i) {
table += "|\t";
currRow = [];
for (let j = 0; j < cols; ++j) {
const rowIdx = i > 1 ? i - 1 : i;
2020-10-20 11:43:11 +00:00
i === 1
? currRow.push("---\t")
: currRow.push(data[`k_${rowIdx}_${j}`] || "");
2020-10-13 00:07:14 +08:00
}
table += currRow.join("\t|\t");
table += "\t|\n";
}
return table;
2020-10-20 11:43:11 +00:00
}
2020-12-04 00:57:46 +08:00
export const toBase64 = file => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result.split(",").pop());
reader.onerror = error => reject(error);
});