优化链式命令的样式,固定每个链式命令的样式

This commit is contained in:
fofolee 2025-01-21 17:04:24 +08:00
parent 020eb2fb06
commit 2728c5783f

View File

@ -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;
}, },
}; };