11<script setup lang="ts">
22import { 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- }
3+ import { useDevToolsPanel } from ' ./hooks/useDevToolsPanel'
4+ import { filterJoin } from ' ./utils'
5+
6+ const {
7+ selectedEl,
8+ renderCssDiffs,
9+ isAllProperty,
10+ handleClearSelection,
11+ onTableCellClassName,
12+ onTableRowClassName,
13+ } = useDevToolsPanel ()
11414 </script >
11515
11616<template >
@@ -129,7 +29,7 @@ function filterJoin(...arg: Array<any>) {
12929 {{ $t('info') }}
13030 </ElText >
13131
132- <ElButton class =" mt-[10px]" type =" warning" plain @click =" clearSelection " >
32+ <ElButton class =" mt-[10px]" type =" warning" plain @click =" handleClearSelection " >
13333 {{ $t('removeBtn') }}
13434 </ElButton >
13535
@@ -144,8 +44,8 @@ function filterJoin(...arg: Array<any>) {
14444 class =" w-full"
14545 border
14646 :data =" renderCssDiffs"
147- :cell-class-name =" tableCellClassName "
148- :row-class-name =" tableRowClassName "
47+ :cell-class-name =" onTableCellClassName "
48+ :row-class-name =" onTableRowClassName "
14949 >
15050 <ElTableColumn prop =" property" :label =" $t('property')" />
15151 <template v-for =" (el ) in selectedEl " :key =" el .valueType " >
0 commit comments