FontStyle.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div class="font-style">
  3. <Button size="small" @click="handleShowColorPicker">
  4. <span class="cus-btn"
  5. ><FontColorsOutlined />
  6. <div class="color-block" :style="{ background: color }"></div>
  7. <ElColorPicker
  8. ref="colorPckerRef"
  9. style="width: 0; height: 0; opacity: 0"
  10. v-model:value="color"
  11. @change="handleColorChange"
  12. >
  13. </ElColorPicker>
  14. </span>
  15. </Button>
  16. <Button size="small" @click="handleBold">
  17. <span class="cus-btn" :class="{ 'active-btn': bold }"
  18. ><BoldOutlined
  19. /></span>
  20. </Button>
  21. <Button size="small" @click="handleItalic">
  22. <span class="cus-btn" :class="{ 'active-btn': italic }"
  23. ><ItalicOutlined
  24. /></span>
  25. </Button>
  26. <InputNumber
  27. size="small"
  28. :value="size"
  29. :min="12"
  30. :step="1"
  31. :precision="0"
  32. style="width: 80px;"
  33. @change="handleValueChange"
  34. >
  35. <template #addonAfter>px</template>
  36. </InputNumber>
  37. </div>
  38. </template>
  39. <script setup lang="ts">
  40. import { defineProps, defineEmits, ref } from "vue";
  41. import {
  42. BoldOutlined,
  43. ItalicOutlined,
  44. FontColorsOutlined,
  45. } from "@ant-design/icons-vue";
  46. import { InputNumber, Button } from "ant-design-vue";
  47. import { ElColorPicker } from "element-plus";
  48. const props = defineProps<{
  49. value: {
  50. size: number;
  51. bold: boolean;
  52. italic: boolean;
  53. color: string;
  54. };
  55. }>();
  56. const emit = defineEmits(["update:value"]);
  57. const bold = ref(props.value?.bold);
  58. const italic = ref(props.value?.italic);
  59. const size = ref(props.value?.size);
  60. const color = ref(props.value?.color);
  61. const colorPckerRef = ref<InstanceType<typeof ElColorPicker>>();
  62. const handleUpdate = () => {
  63. emit("update:value", {
  64. size: size.value,
  65. bold: bold.value,
  66. italic: italic.value,
  67. color: color.value,
  68. });
  69. };
  70. const handleBold = () => {
  71. bold.value = !bold.value;
  72. handleUpdate();
  73. };
  74. const handleItalic = () => {
  75. italic.value = !italic.value;
  76. handleUpdate();
  77. };
  78. const handleColorChange = (val: string) => {
  79. color.value = val;
  80. handleUpdate();
  81. };
  82. const handleValueChange = (val: number) => {
  83. size.value = val;
  84. handleUpdate();
  85. };
  86. const handleShowColorPicker = () => {
  87. colorPckerRef.value?.show();
  88. };
  89. </script>
  90. <style lang="less" scoped>
  91. .font-style {
  92. display: flex;
  93. justify-content: space-between;
  94. }
  95. .active-btn {
  96. color: #1890ff;
  97. }
  98. .color-block {
  99. width: 1em;
  100. height: 2px;
  101. position: absolute;
  102. left: 4px;
  103. bottom: 4px;
  104. border: solid 1px #666;
  105. }
  106. :deep(.ant-btn) {
  107. margin-right: 2px;
  108. padding: 0 4px;
  109. }
  110. :deep(.el-color-picker__trigger) {
  111. display: none;
  112. }
  113. </style>