代码优化:将所有replaceAll方法改为replace方法

This commit is contained in:
街角小林 2025-01-03 15:36:44 +08:00
parent d5d01c5f19
commit 333e5cc878
6 changed files with 47 additions and 43 deletions

View File

@ -338,7 +338,7 @@ class RichText {
// 获取当前正在编辑的内容
getEditText() {
// https://github.com/slab/quill/issues/4509
return this.quill.container.firstChild.innerHTML.replaceAll(/ +/g, match =>
return this.quill.container.firstChild.innerHTML.replace(/ +/g, match =>
' '.repeat(match.length)
)
// 去除ql-cursor节点

View File

@ -293,7 +293,7 @@ class Search {
if (richText) {
return replaceHtmlText(text, searchText, replaceText)
} else {
return text.replaceAll(searchText, replaceText)
return text.replace(new RegExp(searchText, 'g'), replaceText)
}
}

View File

@ -511,7 +511,7 @@ export const loadImage = imgFile => {
// 移除字符串中的html实体
export const removeHTMLEntities = str => {
[[' ', ' ']].forEach(item => {
str = str.replaceAll(item[0], item[1])
str = str.replace(new RegExp(item[0], 'g'), item[1])
})
return str
}
@ -528,7 +528,7 @@ export const isUndef = data => {
// 移除html字符串中节点的内联样式
export const removeHtmlStyle = html => {
return html.replaceAll(/(<[^\s]+)\s+style=["'][^'"]+["']\s*(>)/g, '$1$2')
return html.replace(/(<[^\s]+)\s+style=["'][^'"]+["']\s*(>)/g, '$1$2')
}
// 给html标签中指定的标签添加内联样式
@ -586,7 +586,7 @@ export const replaceHtmlText = (html, searchText, replaceText) => {
// 文本节点
root.replaceChild(
document.createTextNode(
node.nodeValue.replaceAll(searchText, replaceText)
node.nodeValue.replace(new RegExp(searchText, 'g'), replaceText)
),
node
)
@ -613,7 +613,7 @@ export const removeHtmlNodeByClass = (html, selector) => {
// 判断一个颜色是否是白色
export const isWhite = color => {
color = String(color).replaceAll(/\s+/g, '')
color = String(color).replace(/\s+/g, '')
return (
['#fff', '#ffffff', '#FFF', '#FFFFFF', 'rgb(255,255,255)'].includes(
color
@ -623,7 +623,7 @@ export const isWhite = color => {
// 判断一个颜色是否是透明
export const isTransparent = color => {
color = String(color).replaceAll(/\s+/g, '')
color = String(color).replace(/\s+/g, '')
return (
['', 'transparent'].includes(color) || /rgba\(\d+,\d+,\d+,0\)/.test(color)
)
@ -1166,7 +1166,7 @@ export const removeFromParentNodeData = node => {
// 给html自闭合标签添加闭合状态
export const handleSelfCloseTags = str => {
selfCloseTagList.forEach(tagName => {
str = str.replaceAll(
str = str.replace(
new RegExp(`<${tagName}([^>]*)>`, 'g'),
`<${tagName} $1 />`
)
@ -1241,7 +1241,7 @@ export const handleInputPasteText = (e, text) => {
// 去除格式
text = getTextFromHtml(text)
// 去除换行
// text = text.replaceAll(/\n/g, '')
// text = text.replace(/\n/g, '')
const textArr = text.split(/\n/g)
const fragment = document.createDocumentFragment()
textArr.forEach((item, index) => {

View File

@ -4,47 +4,51 @@ const fs = require('fs')
const fileDest = path.join(__dirname, '../src/assets/svg')
const targetDest = path.join(__dirname, '../src/config/image.js')
const run = (dir) => {
let dirs = fs.readdirSync(dir)
dirs.forEach(item => {
let cur = path.join(dir, item)
if (fs.statSync(cur).isDirectory()) {
walkDir(cur, item)
}
})
const run = dir => {
let dirs = fs.readdirSync(dir)
dirs.forEach(item => {
let cur = path.join(dir, item)
if (fs.statSync(cur).isDirectory()) {
walkDir(cur, item)
}
})
}
const list = []
const importList = []
const walkDir = (dir, item) => {
let files = fs.readdirSync(dir)
let name = files.find((file) => {
return !/\./.test(file)
let files = fs.readdirSync(dir)
let name = files.find(file => {
return !/\./.test(file)
})
let fileList = files.filter(file => {
return /\.svg$/.test(file)
})
let itemList = []
fileList.forEach(file => {
let fileName =
item + '_' + file.replace(/\.svg$/, '').replace(new RegExp('-', 'g'), '')
importList.push(`import ${fileName} from '../assets/svg/${item}/${file}'`)
itemList.push({
url: fileName,
width: 100,
height: 100
})
let fileList = files.filter((file) => {
return /\.svg$/.test(file)
})
let itemList = []
fileList.forEach((file) => {
let fileName = item + '_' + file.replace(/\.svg$/, '').replaceAll('-', '')
importList.push(`import ${fileName} from '../assets/svg/${item}/${file}'`)
itemList.push({
url: fileName,
width: 100,
height: 100
})
})
list.push({
name,
list: itemList
})
const content = `
})
list.push({
name,
list: itemList
})
const content = `
// 该文件请运行npm run createNodeImageList命令自动生成
${importList.join('\n')}
export default ${JSON.stringify(list, null, 2).replace(/(url":\s*)"([^"]+)"(,)/g, '$1$2$3')}
export default ${JSON.stringify(list, null, 2).replace(
/(url":\s*)"([^"]+)"(,)/g,
'$1$2$3'
)}
`
fs.writeFileSync(targetDest, content)
fs.writeFileSync(targetDest, content)
}
run(fileDest)
console.log('运行成功')
console.log('运行成功')

View File

@ -142,7 +142,7 @@ export default {
? nodeRichTextToTextWithWrap(root.data.text)
: root.data.text
text = htmlEscape(text)
text = text.replaceAll(/\n/g, '<br>')
text = text.replace(/\n/g, '<br>')
root.textCache = text //
root.label = text
root.uid = root.data.uid

View File

@ -111,7 +111,7 @@ export default {
? nodeRichTextToTextWithWrap(root.data.text)
: root.data.text
text = htmlEscape(text)
text = text.replaceAll(/\n/g, '<br>')
text = text.replace(/\n/g, '<br>')
root.textCache = text //
root.label = text
root.uid = root.data.uid