perf: 使用 nanoid 生成随机 ID

This commit is contained in:
pipipi-pikachu 2022-04-28 20:05:01 +08:00
parent 129afa1f70
commit 565e0d17d6
10 changed files with 34 additions and 48 deletions

6
package-lock.json generated
View File

@ -10385,9 +10385,9 @@
} }
}, },
"nanoid": { "nanoid": {
"version": "3.2.0", "version": "3.3.3",
"resolved": "https://registry.npmmirror.com/nanoid/-/nanoid-3.2.0.tgz", "resolved": "https://registry.npmmirror.com/nanoid/-/nanoid-3.3.3.tgz",
"integrity": "sha512-fmsZYa9lpn69Ad5eDn7FMcnnSR+8R34W9qJEijxYhTbfOWzr22n1QxCMzXLK+ODyW2973V3Fux959iQoUxzUIA==" "integrity": "sha512-p1sjXuopFs0xg+fPASzQ28agW1oHD7xDsd9Xkf3T15H3c/cifrFHVwrh74PdoklAPi+i7MdRsE47vm2r6JoB+w=="
}, },
"nanomatch": { "nanomatch": {
"version": "1.2.13", "version": "1.2.13",

View File

@ -22,6 +22,7 @@
"html-to-image": "^1.9.0", "html-to-image": "^1.9.0",
"lodash": "^4.17.20", "lodash": "^4.17.20",
"mitt": "^3.0.0", "mitt": "^3.0.0",
"nanoid": "^3.3.3",
"pinia": "^2.0.11", "pinia": "^2.0.11",
"pptxgenjs": "^3.10.0", "pptxgenjs": "^3.10.0",
"prosemirror-commands": "^1.1.7", "prosemirror-commands": "^1.1.7",

View File

@ -1,8 +1,8 @@
import { computed } from 'vue' import { computed } from 'vue'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
import { nanoid } from 'nanoid'
import { useMainStore, useSlidesStore } from '@/store' import { useMainStore, useSlidesStore } from '@/store'
import { PPTElement } from '@/types/slides' import { PPTElement } from '@/types/slides'
import { createRandomCode } from '@/utils/common'
import useHistorySnapshot from '@/hooks/useHistorySnapshot' import useHistorySnapshot from '@/hooks/useHistorySnapshot'
export default () => { export default () => {
@ -36,7 +36,7 @@ export default () => {
let newElementList: PPTElement[] = JSON.parse(JSON.stringify(currentSlide.value.elements)) let newElementList: PPTElement[] = JSON.parse(JSON.stringify(currentSlide.value.elements))
// 生成分组ID // 生成分组ID
const groupId = createRandomCode() const groupId = nanoid(10)
// 收集需要组合的元素列表并赋上唯一分组ID // 收集需要组合的元素列表并赋上唯一分组ID
const combineElementList: PPTElement[] = [] const combineElementList: PPTElement[] = []

View File

@ -1,6 +1,6 @@
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
import { nanoid } from 'nanoid'
import { useMainStore, useSlidesStore } from '@/store' import { useMainStore, useSlidesStore } from '@/store'
import { createRandomCode } from '@/utils/common'
import { getImageSize } from '@/utils/image' import { getImageSize } from '@/utils/image'
import { VIEWPORT_SIZE } from '@/configs/canvas' import { VIEWPORT_SIZE } from '@/configs/canvas'
import { PPTLineElement, PPTElement, TableCell, TableCellStyle, PPTShapeElement, PPTChartElement, ChartOptions, PresetChartType } from '@/types/slides' import { PPTLineElement, PPTElement, TableCell, TableCellStyle, PPTShapeElement, PPTChartElement, ChartOptions, PresetChartType } from '@/types/slides'
@ -64,7 +64,7 @@ export default () => {
createElement({ createElement({
type: 'image', type: 'image',
id: createRandomCode(), id: nanoid(10),
src, src,
width, width,
height, height,
@ -83,7 +83,7 @@ export default () => {
const createChartElement = (type: PresetChartType) => { const createChartElement = (type: PresetChartType) => {
const newElement: PPTChartElement = { const newElement: PPTChartElement = {
type: 'chart', type: 'chart',
id: createRandomCode(), id: nanoid(10),
chartType: CHART_TYPES[type], chartType: CHART_TYPES[type],
left: 300, left: 300,
top: 81.25, top: 81.25,
@ -127,7 +127,7 @@ export default () => {
for (let i = 0; i < row; i++) { for (let i = 0; i < row; i++) {
const rowCells: TableCell[] = [] const rowCells: TableCell[] = []
for (let j = 0; j < col; j++) { for (let j = 0; j < col; j++) {
rowCells.push({ id: createRandomCode(), colspan: 1, rowspan: 1, text: '', style }) rowCells.push({ id: nanoid(10), colspan: 1, rowspan: 1, text: '', style })
} }
data.push(rowCells) data.push(rowCells)
} }
@ -142,7 +142,7 @@ export default () => {
createElement({ createElement({
type: 'table', type: 'table',
id: createRandomCode(), id: nanoid(10),
width, width,
height, height,
colWidths, colWidths,
@ -174,7 +174,7 @@ export default () => {
const { left, top, width, height } = position const { left, top, width, height } = position
createElement({ createElement({
type: 'text', type: 'text',
id: createRandomCode(), id: nanoid(10),
left, left,
top, top,
width, width,
@ -195,7 +195,7 @@ export default () => {
const { left, top, width, height } = position const { left, top, width, height } = position
const newElement: PPTShapeElement = { const newElement: PPTShapeElement = {
type: 'shape', type: 'shape',
id: createRandomCode(), id: nanoid(10),
left, left,
top, top,
width, width,
@ -225,7 +225,7 @@ export default () => {
const newElement: PPTLineElement = { const newElement: PPTLineElement = {
type: 'line', type: 'line',
id: createRandomCode(), id: nanoid(10),
left, left,
top, top,
start, start,
@ -248,7 +248,7 @@ export default () => {
const createLatexElement = (data: { path: string; latex: string; w: number; h: number; }) => { const createLatexElement = (data: { path: string; latex: string; w: number; h: number; }) => {
createElement({ createElement({
type: 'latex', type: 'latex',
id: createRandomCode(), id: nanoid(10),
width: data.w, width: data.w,
height: data.h, height: data.h,
rotate: 0, rotate: 0,
@ -270,7 +270,7 @@ export default () => {
const createVideoElement = (src: string) => { const createVideoElement = (src: string) => {
createElement({ createElement({
type: 'video', type: 'video',
id: createRandomCode(), id: nanoid(10),
width: 500, width: 500,
height: 300, height: 300,
rotate: 0, rotate: 0,
@ -287,7 +287,7 @@ export default () => {
const createAudioElement = (src: string) => { const createAudioElement = (src: string) => {
createElement({ createElement({
type: 'audio', type: 'audio',
id: createRandomCode(), id: nanoid(10),
width: 50, width: 50,
height: 50, height: 50,
rotate: 0, rotate: 0,

View File

@ -1,8 +1,8 @@
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
import { nanoid } from 'nanoid'
import { useSlidesStore, useMainStore } from '@/store' import { useSlidesStore, useMainStore } from '@/store'
import { pasteCustomClipboardString } from '@/utils/clipboard' import { pasteCustomClipboardString } from '@/utils/clipboard'
import { PPTElement, Slide } from '@/types/slides' import { PPTElement, Slide } from '@/types/slides'
import { createRandomCode } from '@/utils/common'
import { createElementIdMap } from '@/utils/element' import { createElementIdMap } from '@/utils/element'
import { parseText2Paragraphs } from '@/utils/textParser' import { parseText2Paragraphs } from '@/utils/textParser'
import useHistorySnapshot from '@/hooks/useHistorySnapshot' import useHistorySnapshot from '@/hooks/useHistorySnapshot'
@ -65,7 +65,7 @@ export default () => {
} }
return { return {
...slide, ...slide,
id: createRandomCode(), id: nanoid(10),
} }
}) })
slidesStore.addSlide(newSlides) slidesStore.addSlide(newSlides)

View File

@ -1,8 +1,8 @@
import { computed } from 'vue' import { computed } from 'vue'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
import { nanoid } from 'nanoid'
import { useMainStore, useSlidesStore } from '@/store' import { useMainStore, useSlidesStore } from '@/store'
import { Slide } from '@/types/slides' import { Slide } from '@/types/slides'
import { createRandomCode } from '@/utils/common'
import { copyText, readClipboard } from '@/utils/clipboard' import { copyText, readClipboard } from '@/utils/clipboard'
import { encrypt } from '@/utils/crypto' import { encrypt } from '@/utils/crypto'
import { createElementIdMap } from '@/utils/element' import { createElementIdMap } from '@/utils/element'
@ -27,7 +27,7 @@ export default () => {
// 重置幻灯片 // 重置幻灯片
const resetSlides = () => { const resetSlides = () => {
const emptySlide: Slide = { const emptySlide: Slide = {
id: createRandomCode(), id: nanoid(10),
elements: [], elements: [],
background: { background: {
type: 'solid', type: 'solid',
@ -74,7 +74,7 @@ export default () => {
// 创建一页空白页并添加到下一页 // 创建一页空白页并添加到下一页
const createSlide = () => { const createSlide = () => {
const emptySlide: Slide = { const emptySlide: Slide = {
id: createRandomCode(), id: nanoid(10),
elements: [], elements: [],
background: { background: {
type: 'solid', type: 'solid',
@ -96,7 +96,7 @@ export default () => {
} }
const newSlide = { const newSlide = {
...slide, ...slide,
id: createRandomCode(), id: nanoid(10),
} }
mainStore.setActiveElementIdList([]) mainStore.setActiveElementIdList([])
slidesStore.addSlide(newSlide) slidesStore.addSlide(newSlide)

View File

@ -1,20 +1,5 @@
import { padStart } from 'lodash' import { padStart } from 'lodash'
/**
*
* @param len
*/
export const createRandomCode = (len = 8) => {
const charset = `_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`
const maxLen = charset.length
let ret = ''
for (let i = 0; i < len; i++) {
const randomIndex = Math.floor(Math.random() * maxLen)
ret += charset[randomIndex]
}
return ret
}
/** /**
* *
* @param digit * @param digit

View File

@ -1,6 +1,6 @@
import tinycolor from 'tinycolor2' import tinycolor from 'tinycolor2'
import { nanoid } from 'nanoid'
import { PPTElement, PPTLineElement } from '@/types/slides' import { PPTElement, PPTLineElement } from '@/types/slides'
import { createRandomCode } from '@/utils/common'
interface RotatedElementData { interface RotatedElementData {
left: number; left: number;
@ -164,9 +164,9 @@ export const createElementIdMap = (elements: PPTElement[]) => {
for (const element of elements) { for (const element of elements) {
const groupId = element.groupId const groupId = element.groupId
if (groupId && !groupIdMap[groupId]) { if (groupId && !groupIdMap[groupId]) {
groupIdMap[groupId] = createRandomCode() groupIdMap[groupId] = nanoid(10)
} }
elIdMap[element.id] = createRandomCode() elIdMap[element.id] = nanoid(10)
} }
return { return {
groupIdMap, groupIdMap,

View File

@ -188,9 +188,9 @@
<script lang="ts"> <script lang="ts">
import { computed, defineComponent, onMounted, ref, watch } from 'vue' import { computed, defineComponent, onMounted, ref, watch } from 'vue'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
import { nanoid } from 'nanoid'
import { useMainStore, useSlidesStore } from '@/store' import { useMainStore, useSlidesStore } from '@/store'
import { PPTTableElement, TableCell, TableCellStyle, TableTheme } from '@/types/slides' import { PPTTableElement, TableCell, TableCellStyle, TableTheme } from '@/types/slides'
import { createRandomCode } from '@/utils/common'
import { WEB_FONTS } from '@/configs/font' import { WEB_FONTS } from '@/configs/font'
import useHistorySnapshot from '@/hooks/useHistorySnapshot' import useHistorySnapshot from '@/hooks/useHistorySnapshot'
@ -351,7 +351,7 @@ export default defineComponent({
const rowCount = _handleElement.data.length const rowCount = _handleElement.data.length
if (value > rowCount) { if (value > rowCount) {
const rowCells: TableCell[] = new Array(colCount.value).fill({ id: createRandomCode(), colspan: 1, rowspan: 1, text: '' }) const rowCells: TableCell[] = new Array(colCount.value).fill({ id: nanoid(10), colspan: 1, rowspan: 1, text: '' })
const newTableCells: TableCell[][] = new Array(value - rowCount).fill(rowCells) const newTableCells: TableCell[][] = new Array(value - rowCount).fill(rowCells)
const tableCells: TableCell[][] = JSON.parse(JSON.stringify(_handleElement.data)) const tableCells: TableCell[][] = JSON.parse(JSON.stringify(_handleElement.data))
@ -375,7 +375,7 @@ export default defineComponent({
if (value > colCount) { if (value > colCount) {
tableCells = tableCells.map(item => { tableCells = tableCells.map(item => {
const cells: TableCell[] = new Array(value - colCount).fill({ id: createRandomCode(), colspan: 1, rowspan: 1, text: '' }) const cells: TableCell[] = new Array(value - colCount).fill({ id: nanoid(10), colspan: 1, rowspan: 1, text: '' })
item.push(...cells) item.push(...cells)
return item return item
}) })

View File

@ -70,11 +70,11 @@
import { computed, defineComponent, nextTick, onMounted, onUnmounted, PropType, ref, watch } from 'vue' import { computed, defineComponent, nextTick, onMounted, onUnmounted, PropType, ref, watch } from 'vue'
import { debounce, isEqual } from 'lodash' import { debounce, isEqual } from 'lodash'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
import { nanoid } from 'nanoid'
import { useMainStore } from '@/store' import { useMainStore } from '@/store'
import { PPTElementOutline, TableCell, TableTheme } from '@/types/slides' import { PPTElementOutline, TableCell, TableTheme } from '@/types/slides'
import { ContextmenuItem } from '@/components/Contextmenu/types' import { ContextmenuItem } from '@/components/Contextmenu/types'
import { KEYS } from '@/configs/hotkey' import { KEYS } from '@/configs/hotkey'
import { createRandomCode } from '@/utils/common'
import { getTextStyle, formatText } from './utils' import { getTextStyle, formatText } from './utils'
import useHideCells from './useHideCells' import useHideCells from './useHideCells'
import useSubThemeColor from './useSubThemeColor' import useSubThemeColor from './useSubThemeColor'
@ -332,7 +332,7 @@ export default defineComponent({
colspan: 1, colspan: 1,
rowspan: 1, rowspan: 1,
text: '', text: '',
id: createRandomCode(), id: nanoid(10),
}) })
} }
@ -347,7 +347,7 @@ export default defineComponent({
colspan: 1, colspan: 1,
rowspan: 1, rowspan: 1,
text: '', text: '',
id: createRandomCode(), id: nanoid(10),
} }
item.splice(colIndex, 0, cell) item.splice(colIndex, 0, cell)
return item return item
@ -368,7 +368,7 @@ export default defineComponent({
for (let j = 0; j < _tableCells[0].length; j++) { for (let j = 0; j < _tableCells[0].length; j++) {
rowCells.push({ rowCells.push({
...defaultCell, ...defaultCell,
id: createRandomCode(), id: nanoid(10),
}) })
} }
newRows.push(rowCells) newRows.push(rowCells)
@ -381,7 +381,7 @@ export default defineComponent({
for (let i = 0; i < colCount; i++) { for (let i = 0; i < colCount; i++) {
const cell = { const cell = {
...defaultCell, ...defaultCell,
id: createRandomCode(), id: nanoid(10),
} }
cells.push(cell) cells.push(cell)
} }