将唯一ID生成功能提取到单独的具模块中

This commit is contained in:
fofolee 2025-02-10 22:37:20 +08:00
parent 1a95259aef
commit 039a820952
4 changed files with 17 additions and 18 deletions

View File

@ -252,13 +252,6 @@ export default defineComponent({
.padStart(2, "0") .padStart(2, "0")
); );
}, },
getUniqueId() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
},
}, },
watch: { watch: {
// glassEffect // glassEffect

View File

@ -63,6 +63,7 @@ import ComposerCard from "./ComposerCard.vue";
import ChainStyles from "./flow/ChainStyles.vue"; import ChainStyles from "./flow/ChainStyles.vue";
import DropArea from "./flow/DropArea.vue"; import DropArea from "./flow/DropArea.vue";
import { findCommandByValue } from "js/composer/composerConfig"; import { findCommandByValue } from "js/composer/composerConfig";
import { getUniqueId } from "js/common/uuid";
// //
let commandsBeforeDrag = []; let commandsBeforeDrag = [];
@ -262,7 +263,7 @@ export default defineComponent({
} }
} else { } else {
// //
const chainId = this.getUniqueId(); const chainId = getUniqueId();
let insertIndex = let insertIndex =
this.dragIndex >= 0 ? this.dragIndex : newCommands.length; this.dragIndex >= 0 ? this.dragIndex : newCommands.length;
@ -270,7 +271,7 @@ export default defineComponent({
for (const commandType of commandChain) { for (const commandType of commandChain) {
const commandItem = { const commandItem = {
...newCommand, ...newCommand,
id: this.getUniqueId(), id: getUniqueId(),
commandType, commandType,
chainId, chainId,
}; };
@ -286,13 +287,10 @@ export default defineComponent({
createNewCommand(parsedAction) { createNewCommand(parsedAction) {
const newCommand = { const newCommand = {
...parsedAction, ...parsedAction,
id: this.getUniqueId(), id: getUniqueId(),
}; };
return newCommand; return newCommand;
}, },
getUniqueId() {
return this.$root.getUniqueId();
},
isFirstCommandInChain(command) { isFirstCommandInChain(command) {
if (!command.commandChain) return false; if (!command.commandChain) return false;
return command.commandType === command.commandChain?.[0]; return command.commandType === command.commandChain?.[0];
@ -347,7 +345,7 @@ export default defineComponent({
const newCommands = [...this.commands]; const newCommands = [...this.commands];
const branchCommand = { const branchCommand = {
...window.lodashM.cloneDeep(findCommandByValue(value)), ...window.lodashM.cloneDeep(findCommandByValue(value)),
id: this.getUniqueId(), id: getUniqueId(),
chainId: chainId, chainId: chainId,
commandType: commandType, commandType: commandType,
}; };
@ -409,12 +407,12 @@ export default defineComponent({
}, },
copyCommands(commands) { copyCommands(commands) {
// chainId // chainId
const newChainId = this.getUniqueId(); const newChainId = getUniqueId();
// //
const newCommands = []; const newCommands = [];
commands.forEach((cmd) => { commands.forEach((cmd) => {
const copiedCommand = window.lodashM.cloneDeep(cmd); const copiedCommand = window.lodashM.cloneDeep(cmd);
copiedCommand.id = this.getUniqueId(); copiedCommand.id = getUniqueId();
if (copiedCommand.chainId) copiedCommand.chainId = newChainId; if (copiedCommand.chainId) copiedCommand.chainId = newChainId;
newCommands.push(copiedCommand); newCommands.push(copiedCommand);
}); });
@ -439,7 +437,7 @@ export default defineComponent({
// //
const newCommand = { const newCommand = {
...command, ...command,
id: this.getUniqueId(), id: getUniqueId(),
}; };
const newCommands = [...this.commands]; const newCommands = [...this.commands];
newCommands.splice(index + 1, 0, newCommand); newCommands.splice(index + 1, 0, newCommand);

View File

@ -102,6 +102,7 @@ import FlowManager from "components/composer/flow/FlowManager.vue";
import { generateCode } from "js/composer/generateCode"; import { generateCode } from "js/composer/generateCode";
import { findCommandByValue } from "js/composer/composerConfig"; import { findCommandByValue } from "js/composer/composerConfig";
import { generateUniqSuffix } from "js/composer/variableManager"; import { generateUniqSuffix } from "js/composer/variableManager";
import { getUniqueId } from "js/common/uuid";
export default defineComponent({ export default defineComponent({
name: "FlowTabs", name: "FlowTabs",
components: { components: {
@ -228,7 +229,7 @@ export default defineComponent({
); );
}, },
addFlow(options = {}) { addFlow(options = {}) {
const id = this.$root.getUniqueId(); const id = getUniqueId();
const name = options.name || this.generateFlowName(); const name = options.name || this.generateFlowName();
const newFlow = { const newFlow = {
id, id,

7
src/js/common/uuid.js Normal file
View File

@ -0,0 +1,7 @@
export const getUniqueId = () => {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};