2024-08-18 19:49:16 +08:00
|
|
|
|
import juice from 'juice'
|
2022-02-28 19:09:39 +08:00
|
|
|
|
import prettier from 'prettier/standalone'
|
|
|
|
|
import prettierCss from 'prettier/parser-postcss'
|
2022-08-01 21:19:00 +08:00
|
|
|
|
import prettierMarkdown from 'prettier/parser-markdown'
|
2020-02-15 21:44:42 +08:00
|
|
|
|
|
2024-08-18 19:49:16 +08:00
|
|
|
|
import { defaultTheme, prefix } from '@/config'
|
|
|
|
|
|
|
|
|
|
export function addPrefix(str) {
|
|
|
|
|
return `${prefix}__${str}`
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 11:09:09 +08:00
|
|
|
|
function createCustomTheme(theme, color, isDefault = true) {
|
2022-08-01 21:19:00 +08:00
|
|
|
|
const customTheme = JSON.parse(JSON.stringify(theme))
|
2022-02-28 19:09:39 +08:00
|
|
|
|
customTheme.block.h1[`border-bottom`] = `2px solid ${color}`
|
2024-08-18 19:49:16 +08:00
|
|
|
|
customTheme.block.h2.background = color
|
2022-02-28 19:09:39 +08:00
|
|
|
|
customTheme.block.h3[`border-left`] = `3px solid ${color}`
|
2024-08-18 19:49:16 +08:00
|
|
|
|
customTheme.block.h4.color = color
|
|
|
|
|
customTheme.inline.strong.color = color
|
2024-08-19 11:09:09 +08:00
|
|
|
|
|
|
|
|
|
if (!isDefault) {
|
|
|
|
|
customTheme.block.h3[`border-bottom`] = `1px dashed ${color}`
|
|
|
|
|
customTheme.block.blockquote[`border-left`] = `4px solid ${color}`
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 19:09:39 +08:00
|
|
|
|
return customTheme
|
|
|
|
|
}
|
2020-01-13 22:16:04 +08:00
|
|
|
|
|
2022-08-01 21:19:00 +08:00
|
|
|
|
// 设置自定义颜色
|
|
|
|
|
export function setColorWithTemplate(theme) {
|
|
|
|
|
return (color) => {
|
|
|
|
|
return createCustomTheme(theme, color)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 11:09:09 +08:00
|
|
|
|
export function setColorWithCustomTemplate(theme, color, isDefault = true) {
|
|
|
|
|
return createCustomTheme(theme, color, isDefault)
|
2022-08-01 21:19:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 22:16:04 +08:00
|
|
|
|
// 设置自定义字体大小
|
2020-06-25 17:20:37 +08:00
|
|
|
|
export function setFontSizeWithTemplate(template) {
|
2024-08-19 11:09:09 +08:00
|
|
|
|
return function (fontSize, isDefault = true) {
|
2022-08-01 21:19:00 +08:00
|
|
|
|
const customTheme = JSON.parse(JSON.stringify(template))
|
2024-08-19 11:09:09 +08:00
|
|
|
|
if (isDefault) {
|
|
|
|
|
customTheme.block.h1[`font-size`] = `${fontSize * 1.2}px`
|
|
|
|
|
customTheme.block.h2[`font-size`] = `${fontSize * 1.2}px`
|
|
|
|
|
customTheme.block.h3[`font-size`] = `${fontSize * 1.1}px`
|
|
|
|
|
customTheme.block.h4[`font-size`] = `${fontSize}px`
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
customTheme.block.h1[`font-size`] = `${fontSize * 1.4}px`
|
|
|
|
|
customTheme.block.h2[`font-size`] = `${fontSize * 1.3}px`
|
|
|
|
|
customTheme.block.h3[`font-size`] = `${fontSize * 1.2}px`
|
|
|
|
|
customTheme.block.h4[`font-size`] = `${fontSize * 1.1}px`
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 19:09:39 +08:00
|
|
|
|
return customTheme
|
|
|
|
|
}
|
2020-01-13 22:16:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 11:09:09 +08:00
|
|
|
|
export function setTheme(theme, fontSize, color, isDefault) {
|
|
|
|
|
return setColorWithCustomTemplate(setFontSizeWithTemplate(theme)(fontSize, isDefault), color, isDefault)
|
|
|
|
|
}
|
2020-01-13 22:16:04 +08:00
|
|
|
|
|
2020-06-25 17:20:37 +08:00
|
|
|
|
export function customCssWithTemplate(jsonString, color, theme) {
|
2021-02-28 14:50:52 +08:00
|
|
|
|
// block
|
2022-08-01 21:19:00 +08:00
|
|
|
|
const customTheme = createCustomTheme(theme, color)
|
2022-02-28 19:09:39 +08:00
|
|
|
|
|
|
|
|
|
customTheme.block.h1 = Object.assign(customTheme.block.h1, jsonString.h1)
|
|
|
|
|
customTheme.block.h2 = Object.assign(customTheme.block.h2, jsonString.h2)
|
|
|
|
|
customTheme.block.h3 = Object.assign(customTheme.block.h3, jsonString.h3)
|
|
|
|
|
customTheme.block.h4 = Object.assign(customTheme.block.h4, jsonString.h4)
|
|
|
|
|
customTheme.block.code = Object.assign(
|
|
|
|
|
customTheme.block.code,
|
2024-08-18 19:49:16 +08:00
|
|
|
|
jsonString.code,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
|
|
|
|
customTheme.block.p = Object.assign(customTheme.block.p, jsonString.p)
|
|
|
|
|
customTheme.block.hr = Object.assign(customTheme.block.hr, jsonString.hr)
|
2021-12-01 10:48:49 +08:00
|
|
|
|
customTheme.block.blockquote = Object.assign(
|
|
|
|
|
customTheme.block.blockquote,
|
2024-08-18 19:49:16 +08:00
|
|
|
|
jsonString.blockquote,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
2021-12-01 10:48:49 +08:00
|
|
|
|
customTheme.block.blockquote_p = Object.assign(
|
|
|
|
|
customTheme.block.blockquote_p,
|
2024-08-18 19:49:16 +08:00
|
|
|
|
jsonString.blockquote_p,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
2021-12-01 10:48:49 +08:00
|
|
|
|
customTheme.block.image = Object.assign(
|
|
|
|
|
customTheme.block.image,
|
2024-08-18 19:49:16 +08:00
|
|
|
|
jsonString.image,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
2021-02-28 14:50:52 +08:00
|
|
|
|
|
|
|
|
|
// inline
|
2021-12-01 10:48:49 +08:00
|
|
|
|
customTheme.inline.strong = Object.assign(
|
|
|
|
|
customTheme.inline.strong,
|
2024-08-18 19:49:16 +08:00
|
|
|
|
jsonString.strong,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
2021-12-01 10:48:49 +08:00
|
|
|
|
customTheme.inline.codespan = Object.assign(
|
|
|
|
|
customTheme.inline.codespan,
|
2024-08-18 19:49:16 +08:00
|
|
|
|
jsonString.codespan,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
2021-12-01 10:48:49 +08:00
|
|
|
|
customTheme.inline.link = Object.assign(
|
|
|
|
|
customTheme.inline.link,
|
2024-08-18 19:49:16 +08:00
|
|
|
|
jsonString.link,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
2021-12-01 10:48:49 +08:00
|
|
|
|
customTheme.inline.wx_link = Object.assign(
|
|
|
|
|
customTheme.inline.wx_link,
|
2024-08-18 19:49:16 +08:00
|
|
|
|
jsonString.wx_link,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
|
|
|
|
customTheme.block.ul = Object.assign(customTheme.block.ul, jsonString.ul)
|
|
|
|
|
customTheme.block.ol = Object.assign(customTheme.block.ol, jsonString.ol)
|
2021-12-01 10:48:49 +08:00
|
|
|
|
customTheme.inline.listitem = Object.assign(
|
|
|
|
|
customTheme.inline.listitem,
|
2024-08-18 19:49:16 +08:00
|
|
|
|
jsonString.li,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
|
|
|
|
return customTheme
|
2020-01-13 22:16:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将CSS形式的字符串转换为JSON
|
|
|
|
|
*
|
2022-08-01 21:19:00 +08:00
|
|
|
|
* @param {string} css - css字符串
|
2020-01-13 22:16:04 +08:00
|
|
|
|
*/
|
2020-06-25 17:20:37 +08:00
|
|
|
|
export function css2json(css) {
|
2021-02-28 14:50:52 +08:00
|
|
|
|
// 移除CSS所有注释
|
2022-02-28 19:09:39 +08:00
|
|
|
|
let open, close
|
2021-02-28 14:50:52 +08:00
|
|
|
|
while (
|
2024-08-18 19:49:16 +08:00
|
|
|
|
(open = css.indexOf(`/*`)) !== -1
|
|
|
|
|
&& (close = css.indexOf(`*/`)) !== -1
|
2021-02-28 14:50:52 +08:00
|
|
|
|
) {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
css = css.substring(0, open) + css.substring(close + 2)
|
2021-02-28 14:50:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化返回值
|
2024-08-18 19:49:16 +08:00
|
|
|
|
const json = {}
|
2021-02-28 14:50:52 +08:00
|
|
|
|
|
2024-08-18 19:49:16 +08:00
|
|
|
|
while (css.length > 0 && css.includes(`{`) && css.includes(`}`)) {
|
2021-02-28 14:50:52 +08:00
|
|
|
|
// 存储第一个左/右花括号的下标
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const lbracket = css.indexOf(`{`)
|
|
|
|
|
const rbracket = css.indexOf(`}`)
|
2021-02-28 14:50:52 +08:00
|
|
|
|
|
|
|
|
|
// 第一步:将声明转换为Object,如:
|
|
|
|
|
// `font: 'Times New Roman' 1em; color: #ff0000; margin-top: 1em;`
|
|
|
|
|
// ==>
|
|
|
|
|
// `{"font": "'Times New Roman' 1em", "color": "#ff0000", "margin-top": "1em"}`
|
|
|
|
|
|
|
|
|
|
// 辅助方法:将array转为object
|
2024-08-18 19:49:16 +08:00
|
|
|
|
|
2021-02-28 14:50:52 +08:00
|
|
|
|
function toObject(array) {
|
2024-08-18 19:49:16 +08:00
|
|
|
|
const ret = {}
|
2021-02-28 14:50:52 +08:00
|
|
|
|
array.forEach((e) => {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const index = e.indexOf(`:`)
|
|
|
|
|
const property = e.substring(0, index).trim()
|
2022-08-01 21:19:00 +08:00
|
|
|
|
ret[property] = e.substring(index + 1).trim()
|
2022-02-28 19:09:39 +08:00
|
|
|
|
})
|
|
|
|
|
return ret
|
2020-01-13 22:16:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 14:50:52 +08:00
|
|
|
|
// 切割声明块并移除空白符,然后放入数组中
|
|
|
|
|
let declarations = css
|
|
|
|
|
.substring(lbracket + 1, rbracket)
|
2022-02-28 19:09:39 +08:00
|
|
|
|
.split(`;`)
|
2024-08-18 19:49:16 +08:00
|
|
|
|
.map(e => e.trim())
|
|
|
|
|
.filter(e => e.length > 0) // 移除所有""空值
|
2021-02-28 14:50:52 +08:00
|
|
|
|
|
|
|
|
|
// 转为Object对象
|
2022-02-28 19:09:39 +08:00
|
|
|
|
declarations = toObject(declarations)
|
2021-02-28 14:50:52 +08:00
|
|
|
|
|
|
|
|
|
// 第二步:选择器处理,每个选择器会与它对应的声明相关联,如:
|
|
|
|
|
// `h1, p#bar {color: red}`
|
|
|
|
|
// ==>
|
|
|
|
|
// {"h1": {color: red}, "p#bar": {color: red}}
|
|
|
|
|
|
2024-08-18 19:49:16 +08:00
|
|
|
|
const selectors = css
|
2021-02-28 14:50:52 +08:00
|
|
|
|
.substring(0, lbracket)
|
|
|
|
|
// 以,切割,并移除空格:`"h1, p#bar, span.foo"` => ["h1", "p#bar", "span.foo"]
|
2022-02-28 19:09:39 +08:00
|
|
|
|
.split(`,`)
|
2024-08-18 19:49:16 +08:00
|
|
|
|
.map(selector => selector.trim())
|
2021-02-28 14:50:52 +08:00
|
|
|
|
|
|
|
|
|
// 迭代赋值
|
|
|
|
|
selectors.forEach((selector) => {
|
|
|
|
|
// 若不存在,则先初始化
|
2024-08-18 19:49:16 +08:00
|
|
|
|
if (!json[selector])
|
|
|
|
|
json[selector] = {}
|
2021-02-28 14:50:52 +08:00
|
|
|
|
// 赋值到JSON
|
|
|
|
|
Object.keys(declarations).forEach((key) => {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
json[selector][key] = declarations[key]
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-01-13 22:16:04 +08:00
|
|
|
|
|
2021-02-28 14:50:52 +08:00
|
|
|
|
// 继续下个声明块
|
2022-02-28 19:09:39 +08:00
|
|
|
|
css = css.slice(rbracket + 1).trim()
|
2021-02-28 14:50:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回JSON形式的结果串
|
2022-02-28 19:09:39 +08: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) {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const content = editor.getValue(0)
|
2021-02-28 14:50:52 +08:00
|
|
|
|
if (content) {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
localStorage.setItem(name, content)
|
2024-08-18 19:49:16 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
localStorage.removeItem(name)
|
2021-02-28 14:50:52 +08:00
|
|
|
|
}
|
2020-05-01 21:30:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 09:33:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* 格式化文档
|
2022-08-01 21:19:00 +08:00
|
|
|
|
* @param {string} content - 文档内容
|
2020-11-21 09:33:45 +08:00
|
|
|
|
*/
|
2020-05-31 17:30:44 +08:00
|
|
|
|
export function formatDoc(content) {
|
2022-08-01 21:19:00 +08:00
|
|
|
|
return prettier.format(content, {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
parser: `markdown`,
|
2021-02-28 14:50:52 +08:00
|
|
|
|
plugins: [prettierMarkdown],
|
2022-02-28 19:09:39 +08:00
|
|
|
|
})
|
2020-06-25 17:20:37 +08:00
|
|
|
|
}
|
2020-07-04 00:55:40 +08:00
|
|
|
|
|
2020-12-03 00:51:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* 格式化css
|
2022-08-01 21:19:00 +08:00
|
|
|
|
* @param {string} content - css内容
|
2020-12-03 00:51:39 +08:00
|
|
|
|
*/
|
|
|
|
|
export function formatCss(content) {
|
2022-08-01 21:19:00 +08:00
|
|
|
|
return prettier.format(content, {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
parser: `css`,
|
2021-02-28 14:50:52 +08:00
|
|
|
|
plugins: [prettierCss],
|
2022-02-28 19:09:39 +08:00
|
|
|
|
})
|
2020-12-03 00:51:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 09:33:45 +08:00
|
|
|
|
/**
|
2021-11-23 21:45:53 +08:00
|
|
|
|
* 导出原始 Markdown 文档
|
2022-08-01 21:19:00 +08:00
|
|
|
|
* @param {string} doc - 文档内容
|
2020-11-21 09:33:45 +08:00
|
|
|
|
*/
|
2020-11-22 22:36:35 +08:00
|
|
|
|
export function downloadMD(doc) {
|
2022-08-01 21:19:00 +08:00
|
|
|
|
const downLink = document.createElement(`a`)
|
2020-07-13 00:26:29 +08:00
|
|
|
|
|
2022-02-28 19:09:39 +08:00
|
|
|
|
downLink.download = `content.md`
|
|
|
|
|
downLink.style.display = `none`
|
2022-08-01 21:19:00 +08:00
|
|
|
|
const blob = new Blob([doc])
|
2020-07-13 00:26:29 +08:00
|
|
|
|
|
2022-02-28 19:09:39 +08:00
|
|
|
|
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
|
|
|
|
|
2021-10-14 20:12:19 +08:00
|
|
|
|
/**
|
|
|
|
|
* 导出 HTML 生成内容
|
|
|
|
|
*/
|
2021-10-30 22:54:41 +08:00
|
|
|
|
export function exportHTML() {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const element = document.querySelector(`#output`)
|
|
|
|
|
setStyles(element)
|
|
|
|
|
const htmlStr = element.innerHTML
|
2021-10-30 22:54:41 +08:00
|
|
|
|
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const downLink = document.createElement(`a`)
|
2021-10-14 20:12:19 +08:00
|
|
|
|
|
2022-02-28 19:09:39 +08:00
|
|
|
|
downLink.download = `content.html`
|
|
|
|
|
downLink.style.display = `none`
|
2024-08-18 19:49:16 +08:00
|
|
|
|
const blob = new Blob([
|
2021-10-16 09:54:45 +08:00
|
|
|
|
`<html><head><meta charset="utf-8" /></head><body><div style="width: 750px; margin: auto;">${htmlStr}</div></body></html>`,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
])
|
2021-10-14 20:12:19 +08:00
|
|
|
|
|
2022-02-28 19:09:39 +08:00
|
|
|
|
downLink.href = URL.createObjectURL(blob)
|
|
|
|
|
document.body.appendChild(downLink)
|
|
|
|
|
downLink.click()
|
|
|
|
|
document.body.removeChild(downLink)
|
2021-10-30 22:54:41 +08:00
|
|
|
|
|
|
|
|
|
function setStyles(element) {
|
2022-08-01 21:19:00 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取一个 DOM 元素的所有样式,
|
|
|
|
|
* @param {DOM 元素} element DOM 元素
|
|
|
|
|
* @param {排除的属性} excludes 如果某些属性对结果有不良影响,可以使用这个参数来排除
|
|
|
|
|
* @returns 行内样式拼接结果
|
|
|
|
|
*/
|
|
|
|
|
function getElementStyles(element, excludes = [`width`, `height`]) {
|
|
|
|
|
const styles = getComputedStyle(element, null)
|
|
|
|
|
return Object.entries(styles)
|
|
|
|
|
.filter(
|
2024-08-18 19:49:16 +08:00
|
|
|
|
([key]) => styles.getPropertyValue(key) && !excludes.includes(key),
|
2022-08-01 21:19:00 +08:00
|
|
|
|
)
|
|
|
|
|
.map(([key, value]) => `${key}:${value};`)
|
|
|
|
|
.join(``)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-30 22:54:41 +08:00
|
|
|
|
switch (true) {
|
|
|
|
|
case isPre(element):
|
|
|
|
|
case isCode(element):
|
|
|
|
|
case isSpan(element):
|
2022-02-28 19:09:39 +08:00
|
|
|
|
element.setAttribute(`style`, getElementStyles(element))
|
|
|
|
|
// eslint-disable-next-line no-fallthrough
|
2021-10-30 22:54:41 +08:00
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
if (element.children.length) {
|
2024-08-18 19:49:16 +08:00
|
|
|
|
Array.from(element.children).forEach(child => setStyles(child))
|
2021-10-30 22:54:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断是否是包裹代码块的 pre 元素
|
|
|
|
|
function isPre(element) {
|
|
|
|
|
return (
|
2024-08-18 19:49:16 +08:00
|
|
|
|
element.tagName === `PRE`
|
|
|
|
|
&& Array.from(element.classList).includes(`code__pre`)
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
2021-10-30 22:54:41 +08:00
|
|
|
|
}
|
2022-08-01 21:19:00 +08:00
|
|
|
|
|
2021-10-30 22:54:41 +08:00
|
|
|
|
// 判断是否是包裹代码块的 code 元素
|
|
|
|
|
function isCode(element) {
|
2024-07-23 22:41:02 +08:00
|
|
|
|
return element.tagName === `CODE`
|
2021-10-30 22:54:41 +08:00
|
|
|
|
}
|
2022-08-01 21:19:00 +08:00
|
|
|
|
|
2021-10-30 22:54:41 +08:00
|
|
|
|
// 判断是否是包裹代码字符的 span 元素
|
|
|
|
|
function isSpan(element) {
|
|
|
|
|
return (
|
2024-08-18 19:49:16 +08:00
|
|
|
|
element.tagName === `SPAN`
|
|
|
|
|
&& (isCode(element.parentElement)
|
|
|
|
|
|| isCode(element.parentElement.parentElement))
|
2022-02-28 19:09:39 +08:00
|
|
|
|
)
|
2021-10-30 22:54:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-14 20:12:19 +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 }) {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
let table = ``
|
2021-02-28 14:50:52 +08:00
|
|
|
|
for (let i = 0; i < rows + 2; ++i) {
|
2022-07-27 14:09:06 +08:00
|
|
|
|
table += `| `
|
|
|
|
|
const currRow = []
|
2021-02-28 14:50:52 +08:00
|
|
|
|
for (let j = 0; j < cols; ++j) {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const rowIdx = i > 1 ? i - 1 : i
|
2022-07-27 14:09:06 +08:00
|
|
|
|
currRow.push(i === 1 ? `---` : data[`k_${rowIdx}_${j}`] || ` `)
|
2020-10-13 00:07:14 +08:00
|
|
|
|
}
|
2022-07-27 14:09:06 +08:00
|
|
|
|
table += currRow.join(` | `)
|
|
|
|
|
table += ` |\n`
|
2021-02-28 14:50:52 +08:00
|
|
|
|
}
|
2020-10-13 00:07:14 +08:00
|
|
|
|
|
2022-02-28 19:09:39 +08:00
|
|
|
|
return table
|
2020-10-20 11:43:11 +00:00
|
|
|
|
}
|
2020-12-04 00:57:46 +08:00
|
|
|
|
|
2022-08-01 21:19:00 +08:00
|
|
|
|
export function toBase64(file) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const reader = new FileReader()
|
|
|
|
|
reader.readAsDataURL(file)
|
|
|
|
|
reader.onload = () => resolve(reader.result.split(`,`).pop())
|
2024-08-18 19:49:16 +08:00
|
|
|
|
reader.onerror = error => reject(error)
|
2022-02-28 19:09:39 +08:00
|
|
|
|
})
|
2022-08-01 21:19:00 +08:00
|
|
|
|
}
|
2020-12-05 21:16:09 +08:00
|
|
|
|
|
|
|
|
|
export function checkImage(file) {
|
2021-02-28 14:50:52 +08:00
|
|
|
|
// check filename suffix
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const isValidSuffix = /\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.name)
|
2021-02-28 14:50:52 +08:00
|
|
|
|
if (!isValidSuffix) {
|
|
|
|
|
return {
|
|
|
|
|
ok: false,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
msg: `请上传 JPG/PNG/GIF 格式的图片`,
|
|
|
|
|
}
|
2021-02-28 14:50:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check file size
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const maxSize = 10
|
|
|
|
|
const valid = file.size / 1024 / 1024 <= maxSize
|
2021-11-26 23:42:56 +08:00
|
|
|
|
if (!valid) {
|
2021-02-28 14:50:52 +08:00
|
|
|
|
return {
|
|
|
|
|
ok: false,
|
|
|
|
|
msg: `由于公众号限制,图片大小不能超过 ${maxSize}M`,
|
2022-02-28 19:09:39 +08:00
|
|
|
|
}
|
2021-02-28 14:50:52 +08:00
|
|
|
|
}
|
2022-02-28 19:09:39 +08:00
|
|
|
|
return { ok: true }
|
2020-12-05 21:16:09 +08:00
|
|
|
|
}
|
2021-10-30 22:54:41 +08:00
|
|
|
|
|
2021-11-24 14:31:19 +08:00
|
|
|
|
/**
|
|
|
|
|
* 移除左边多余空格
|
2021-11-26 23:42:56 +08:00
|
|
|
|
* @param {*} str
|
2024-08-18 19:49:16 +08:00
|
|
|
|
* @returns string
|
2021-11-24 14:31:19 +08:00
|
|
|
|
*/
|
|
|
|
|
export function removeLeft(str) {
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const lines = str.split(`\n`)
|
2021-11-24 14:31:19 +08:00
|
|
|
|
// 获取应该删除的空白符数量
|
2022-02-28 19:09:39 +08:00
|
|
|
|
const minSpaceNum = lines
|
2024-08-18 19:49:16 +08:00
|
|
|
|
.filter(item => item.trim())
|
|
|
|
|
.map(item => item.match(/(^\s+)?/)[0].length)
|
2021-11-24 14:31:19 +08:00
|
|
|
|
.sort((a, b) => a - b)[0]
|
|
|
|
|
// 删除空白符
|
2024-08-18 19:49:16 +08:00
|
|
|
|
return lines.map(item => item.slice(minSpaceNum)).join(`\n`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function solveWeChatImage() {
|
|
|
|
|
const clipboardDiv = document.getElementById(`output`)
|
|
|
|
|
const images = clipboardDiv.getElementsByTagName(`img`)
|
|
|
|
|
for (let i = 0; i < images.length; i++) {
|
|
|
|
|
const image = images[i]
|
|
|
|
|
const width = image.getAttribute(`width`)
|
|
|
|
|
const height = image.getAttribute(`height`)
|
|
|
|
|
image.removeAttribute(`width`)
|
|
|
|
|
image.removeAttribute(`height`)
|
|
|
|
|
image.style.width = width
|
|
|
|
|
image.style.height = height
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function mergeCss(html) {
|
|
|
|
|
return juice(html, {
|
|
|
|
|
inlinePseudoElements: true,
|
|
|
|
|
preserveImportant: true,
|
|
|
|
|
})
|
2021-11-26 23:42:56 +08:00
|
|
|
|
}
|