mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-08 22:51:25 +08:00
优化链式命令的样式,固定每个链式命令的样式
This commit is contained in:
parent
020eb2fb06
commit
2728c5783f
@ -3,11 +3,10 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { defineComponent, ref, watchEffect, computed } from "vue";
|
import { defineComponent, computed } from "vue";
|
||||||
|
|
||||||
// 样式常量
|
// 样式常量
|
||||||
const STYLE_CONSTANTS = {
|
const STYLE_CONSTANTS = {
|
||||||
goldenRatio: 0.618033988749895,
|
|
||||||
hueStep: 360 * 0.618033988749895,
|
hueStep: 360 * 0.618033988749895,
|
||||||
indent: 5,
|
indent: 5,
|
||||||
lightSl: "70%, 60%",
|
lightSl: "70%, 60%",
|
||||||
@ -15,6 +14,15 @@ const STYLE_CONSTANTS = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 工具函数
|
// 工具函数
|
||||||
|
const hashString = (str) => {
|
||||||
|
let hash = 0;
|
||||||
|
for (let i = 0; i < str.length; i++) {
|
||||||
|
hash = (hash << 5) - hash + str.charCodeAt(i);
|
||||||
|
hash = hash & hash;
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
|
};
|
||||||
|
|
||||||
const generateStyleString = (selector, rules) => {
|
const generateStyleString = (selector, rules) => {
|
||||||
return `${selector} {
|
return `${selector} {
|
||||||
${Object.entries(rules)
|
${Object.entries(rules)
|
||||||
@ -26,18 +34,11 @@ ${Object.entries(rules)
|
|||||||
}`;
|
}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const generateShadows = (
|
const generateShadows = (parentChainIds, hue, indent, lightSl, darkSl) => {
|
||||||
parentChainIds,
|
|
||||||
hue,
|
|
||||||
indent,
|
|
||||||
lightSl,
|
|
||||||
darkSl,
|
|
||||||
uniqueChainIds
|
|
||||||
) => {
|
|
||||||
return parentChainIds.reduce(
|
return parentChainIds.reduce(
|
||||||
(acc, parentChainId, i) => {
|
(acc, parentChainId, i) => {
|
||||||
const parentIndex = uniqueChainIds.indexOf(parentChainId);
|
const parentHue =
|
||||||
const parentHue = (parentIndex * STYLE_CONSTANTS.hueStep) % 360;
|
(Math.abs(hashString(parentChainId)) * STYLE_CONSTANTS.hueStep) % 360;
|
||||||
const start = -((i + 2) * indent);
|
const start = -((i + 2) * indent);
|
||||||
acc.light.push(`${start}px 0 0 0 hsl(${parentHue}, ${lightSl})`);
|
acc.light.push(`${start}px 0 0 0 hsl(${parentHue}, ${lightSl})`);
|
||||||
acc.dark.push(`${start}px 0 0 0 hsl(${parentHue}, ${darkSl})`);
|
acc.dark.push(`${start}px 0 0 0 hsl(${parentHue}, ${darkSl})`);
|
||||||
@ -88,15 +89,18 @@ export default defineComponent({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
setup(props) {
|
setup(props) {
|
||||||
const styles = ref("");
|
|
||||||
const chainGroups = computed(() => getChainGroups(props.commands));
|
const chainGroups = computed(() => getChainGroups(props.commands));
|
||||||
|
|
||||||
// 使用 watchEffect 监听 commands 变化并重新计算样式
|
// 根据 chainId 获取固定的色相值
|
||||||
watchEffect(() => {
|
const getChainHue = (chainId) => {
|
||||||
// 如果 commands 为空,不生成样式
|
const hash = Math.abs(hashString(chainId));
|
||||||
|
return (hash * STYLE_CONSTANTS.hueStep) % 360;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 将样式生成逻辑移到 computed 中
|
||||||
|
const styles = computed(() => {
|
||||||
if (!props.commands?.length) {
|
if (!props.commands?.length) {
|
||||||
styles.value = "";
|
return "";
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -117,10 +121,11 @@ export default defineComponent({
|
|||||||
|
|
||||||
// 3. 生成样式
|
// 3. 生成样式
|
||||||
const styleRules = {};
|
const styleRules = {};
|
||||||
const { hueStep, indent, lightSl, darkSl } = STYLE_CONSTANTS;
|
const { indent, lightSl, darkSl } = STYLE_CONSTANTS;
|
||||||
|
|
||||||
uniqueChainIds.forEach((chainId, index) => {
|
uniqueChainIds.forEach((chainId) => {
|
||||||
const hue = (index * hueStep) % 360;
|
// 使用固定的色相值
|
||||||
|
const hue = getChainHue(chainId);
|
||||||
const className = "chain-group-" + chainId;
|
const className = "chain-group-" + chainId;
|
||||||
|
|
||||||
// 计算深度
|
// 计算深度
|
||||||
@ -145,8 +150,7 @@ export default defineComponent({
|
|||||||
hue,
|
hue,
|
||||||
indent,
|
indent,
|
||||||
lightSl,
|
lightSl,
|
||||||
darkSl,
|
darkSl
|
||||||
uniqueChainIds
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const commonStyle = {
|
const commonStyle = {
|
||||||
@ -173,12 +177,12 @@ export default defineComponent({
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 4. 生成最终的样式字符串
|
// 4. 生成最终的样式字符串
|
||||||
styles.value = Object.entries(styleRules)
|
return Object.entries(styleRules)
|
||||||
.map(([selector, rules]) => generateStyleString(selector, rules))
|
.map(([selector, rules]) => generateStyleString(selector, rules))
|
||||||
.join("\n\n");
|
.join("\n\n");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error generating chain styles:", error);
|
console.error("Error generating chain styles:", error);
|
||||||
styles.value = "";
|
return "";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -186,8 +190,6 @@ export default defineComponent({
|
|||||||
styles,
|
styles,
|
||||||
getChainGroupClass(index) {
|
getChainGroupClass(index) {
|
||||||
const classes = {};
|
const classes = {};
|
||||||
|
|
||||||
// 使用计算好的链组
|
|
||||||
for (const group of chainGroups.value) {
|
for (const group of chainGroups.value) {
|
||||||
if (index >= group.startIndex && index <= group.endIndex) {
|
if (index >= group.startIndex && index <= group.endIndex) {
|
||||||
classes[`chain-group-${group.chainId}`] = true;
|
classes[`chain-group-${group.chainId}`] = true;
|
||||||
@ -204,7 +206,6 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return classes;
|
return classes;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user