|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ElBacktop, ElButton, ElCheckbox, ElIcon, ElOption, ElSelect, ElTable, ElTableColumn, ElText, ElTooltip } from 'element-plus' |
| 3 | +import { computed, onMounted, reactive, ref } from 'vue' |
| 4 | +import { formatStyle, type FormatStyleValue } from './formatStyle' |
| 5 | +
|
| 6 | +type SelectedElType = { |
| 7 | + valueType: 'left' | 'right' |
| 8 | +} & FormatStyleValue |
| 9 | +
|
| 10 | +interface CssDiffsType { |
| 11 | + property: string |
| 12 | + left: string |
| 13 | + right: string |
| 14 | + isDiff: boolean |
| 15 | +} |
| 16 | +
|
| 17 | +const selectedEl: Array<SelectedElType> = reactive([]) |
| 18 | +const cssDiffs: Array<CssDiffsType> = reactive([]) |
| 19 | +
|
| 20 | +const isAllProperty = ref(false) |
| 21 | +const isLoadComplete = ref(false) |
| 22 | +
|
| 23 | +onMounted(() => { |
| 24 | + chrome.devtools.panels.elements.onSelectionChanged.addListener(() => { |
| 25 | + chrome.devtools.inspectedWindow.eval( |
| 26 | + `(() => document.readyState)($0)`, |
| 27 | + (readyState: Document['readyState']) => { |
| 28 | + if (readyState === 'complete') { |
| 29 | + isLoadComplete.value = true |
| 30 | + } |
| 31 | + else { |
| 32 | + isLoadComplete.value = false |
| 33 | + } |
| 34 | + }, |
| 35 | + ) |
| 36 | +
|
| 37 | + chrome.devtools.inspectedWindow.eval( |
| 38 | + `(${formatStyle.toString()})($0)`, |
| 39 | + (result: FormatStyleValue, isException) => { |
| 40 | + if (!isException && result != null && isLoadComplete.value) { |
| 41 | + saveSelectedEl(result) |
| 42 | + } |
| 43 | + }, |
| 44 | + ) |
| 45 | + }) |
| 46 | +}) |
| 47 | +
|
| 48 | +function saveSelectedEl(result: FormatStyleValue) { |
| 49 | + if (selectedEl.length === 2) |
| 50 | + return |
| 51 | +
|
| 52 | + const valueType = !selectedEl.length ? 'left' : 'right' |
| 53 | + selectedEl.push({ ...result, valueType }) |
| 54 | +
|
| 55 | + if (selectedEl.length === 2) { |
| 56 | + compareSelectedEl() |
| 57 | + } |
| 58 | +} |
| 59 | +
|
| 60 | +function clearSelection() { |
| 61 | + selectedEl.length = 0 |
| 62 | + cssDiffs.length = 0 |
| 63 | +} |
| 64 | +
|
| 65 | +function compareSelectedEl() { |
| 66 | + const [{ style: styles1 = {} }, { style: styles2 = {} }] = selectedEl |
| 67 | +
|
| 68 | + const diffs: Array<CssDiffsType> = [] |
| 69 | +
|
| 70 | + const allProperties = new Set([ |
| 71 | + ...Object.keys(styles1), |
| 72 | + ...Object.keys(styles2), |
| 73 | + ]) |
| 74 | +
|
| 75 | + allProperties.forEach((property) => { |
| 76 | + const left = styles1[property] || '未定义' |
| 77 | + const right = styles2[property] || '未定义' |
| 78 | +
|
| 79 | + diffs.push({ |
| 80 | + property, |
| 81 | + left, |
| 82 | + right, |
| 83 | + isDiff: left !== right, |
| 84 | + }) |
| 85 | + }) |
| 86 | +
|
| 87 | + cssDiffs.push(...diffs) |
| 88 | +} |
| 89 | +
|
| 90 | +const renderCssDiffs = computed(() => { |
| 91 | + return isAllProperty.value |
| 92 | + ? cssDiffs |
| 93 | + : cssDiffs.filter(css => css.isDiff) |
| 94 | +}) |
| 95 | +
|
| 96 | +function tableCellClassName({ columnIndex }: { columnIndex: number }) { |
| 97 | + if (!columnIndex) { |
| 98 | + return 'text-[var(--el-table-text-color)]' |
| 99 | + } |
| 100 | +} |
| 101 | +
|
| 102 | +function tableRowClassName({ row }: { row: CssDiffsType }) { |
| 103 | + if (row.isDiff) { |
| 104 | + return '!bg-[#ffe6e6] text-[red]' |
| 105 | + } |
| 106 | + else { |
| 107 | + return '!bg-[#e6ffe6] text-[green]' |
| 108 | + } |
| 109 | +} |
| 110 | +
|
| 111 | +function filterJoin(...arg: Array<any>) { |
| 112 | + return arg.filter(Boolean).join(' $$ ') |
| 113 | +} |
| 114 | +</script> |
| 115 | + |
| 116 | +<template> |
| 117 | + <div class="relative"> |
| 118 | + <h2 class="font-bold text-center text-lg"> |
| 119 | + DOM Diff |
| 120 | + </h2> |
| 121 | + |
| 122 | + <ElSelect v-model="$i18n.locale" class="!w-[100px] !absolute !right-0 !top-0" size="small"> |
| 123 | + <ElOption v-for="locale in $i18n.availableLocales" :key="`locale-${locale}`" :value="locale"> |
| 124 | + {{ locale }} |
| 125 | + </ElOption> |
| 126 | + </ElSelect> |
| 127 | + |
| 128 | + <ElText class="block" type="primary"> |
| 129 | + {{ $t('info') }} |
| 130 | + </ElText> |
| 131 | + |
| 132 | + <ElButton class="mt-[10px]" type="warning" plain @click="clearSelection"> |
| 133 | + {{ $t('removeBtn') }} |
| 134 | + </ElButton> |
| 135 | + |
| 136 | + <ElText v-if="!renderCssDiffs.length" class="block !mt-[10px]" type="danger"> |
| 137 | + {{ $t('selectedInfo') }} |
| 138 | + </ElText> |
| 139 | + |
| 140 | + <div class="flex justify-end"> |
| 141 | + <ElCheckbox v-model="isAllProperty" :label="$t('isAllProperty')" /> |
| 142 | + </div> |
| 143 | + <ElTable |
| 144 | + class="w-full" |
| 145 | + border |
| 146 | + :data="renderCssDiffs" |
| 147 | + :cell-class-name="tableCellClassName" |
| 148 | + :row-class-name="tableRowClassName" |
| 149 | + > |
| 150 | + <ElTableColumn prop="property" :label="$t('property')" /> |
| 151 | + <template v-for="(el) in selectedEl" :key="el.valueType"> |
| 152 | + <ElTableColumn :prop="el.valueType"> |
| 153 | + <template #header> |
| 154 | + <span class="align-middle">{{ filterJoin(el.tag, el.id, el.class) }}</span> |
| 155 | + |
| 156 | + <ElTooltip :content="$t('tableColumnInfo')" trigger="click"> |
| 157 | + <ElIcon class="align-middle ml-[4px]"> |
| 158 | + <svg xmlns="http://www.w3.org/2000/svg" width="1024" height="1024" viewBox="0 0 1024 1024"><path fill="currentColor" d="M512 64a448 448 0 1 1 0 896a448 448 0 0 1 0-896m0 832a384 384 0 0 0 0-768a384 384 0 0 0 0 768m48-176a48 48 0 1 1-96 0a48 48 0 0 1 96 0m-48-464a32 32 0 0 1 32 32v288a32 32 0 0 1-64 0V288a32 32 0 0 1 32-32" /></svg> |
| 159 | + </ElIcon> |
| 160 | + </ElTooltip> |
| 161 | + </template> |
| 162 | + </ElTableColumn> |
| 163 | + </template> |
| 164 | + </ElTable> |
| 165 | + |
| 166 | + <ElBacktop :right="20" :bottom="30" /> |
| 167 | + </div> |
| 168 | +</template> |
0 commit comments