dsh-design-studio 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/lib/client.js +313 -31
- package/lib/doc.js +118 -46
- package/lib/index.js +29 -9
- package/lib/look.js +32 -2
- package/lib/picker.js +275 -3
- package/package.json +1 -1
package/lib/picker.js
CHANGED
|
@@ -993,6 +993,245 @@
|
|
|
993
993
|
|
|
994
994
|
const seedNow = () => ({ href: here(), scroll: { x: scrollX, y: scrollY }, sig: snapshot().map })
|
|
995
995
|
|
|
996
|
+
/** 选择器列表按顶层逗号拆开(括号、方括号里的逗号不算) */
|
|
997
|
+
function splitSelectors(text) {
|
|
998
|
+
const out = []
|
|
999
|
+
let depth = 0
|
|
1000
|
+
let from = 0
|
|
1001
|
+
for (let i = 0; i < text.length; i++) {
|
|
1002
|
+
const c = text[i]
|
|
1003
|
+
if (c === '\\') i++
|
|
1004
|
+
else if (c === '(' || c === '[') depth++
|
|
1005
|
+
else if (c === ')' || c === ']') depth--
|
|
1006
|
+
else if (c === ',' && !depth) { out.push(text.slice(from, i)); from = i + 1 }
|
|
1007
|
+
}
|
|
1008
|
+
out.push(text.slice(from))
|
|
1009
|
+
return out
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
/** 去掉 :where / :is / :not / :has(...),括号按配对找(可以套好几层);fn 收到名字和括号里的内容 */
|
|
1013
|
+
function stripFns(s, fn) {
|
|
1014
|
+
const re = /:(where|is|not|has|matches|-webkit-any)\(/g
|
|
1015
|
+
let out = ''
|
|
1016
|
+
let from = 0
|
|
1017
|
+
let m
|
|
1018
|
+
while ((m = re.exec(s))) {
|
|
1019
|
+
let depth = 1
|
|
1020
|
+
let j = re.lastIndex
|
|
1021
|
+
for (; j < s.length && depth; j++) { if (s[j] === '(') depth++; else if (s[j] === ')') depth-- }
|
|
1022
|
+
out += s.slice(from, m.index)
|
|
1023
|
+
fn(m[1], s.slice(re.lastIndex, j - 1))
|
|
1024
|
+
from = j
|
|
1025
|
+
re.lastIndex = j
|
|
1026
|
+
}
|
|
1027
|
+
return out + s.slice(from)
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
/** 一个选择器的优先级 [id, 类 / 属性 / 伪类, 元素 / 伪元素];:where 不算,:is / :not / :has 按括号里最高的算 */
|
|
1031
|
+
function specificity(sel) {
|
|
1032
|
+
let [a, b, c] = [0, 0, 0]
|
|
1033
|
+
let s = stripFns(sel.replace(/\\./g, '_'), (name, inner) => {
|
|
1034
|
+
if (name === 'where') return
|
|
1035
|
+
const top = splitSelectors(inner).map(specificity).reduce((x, y) => (rankCmp(x, y) >= 0 ? x : y), [0, 0, 0])
|
|
1036
|
+
a += top[0]; b += top[1]; c += top[2]
|
|
1037
|
+
})
|
|
1038
|
+
s = s.replace(/\[[^\]]*\]/g, () => { b++; return '' })
|
|
1039
|
+
s = s.replace(/#[^\s.#:[\]>+~,()]+/g, () => { a++; return '' })
|
|
1040
|
+
s = s.replace(/\.[^\s.#:[\]>+~,()]+/g, () => { b++; return '' })
|
|
1041
|
+
s = s.replace(/::[\w-]+(?:\([^)]*\))?/g, () => { c++; return '' })
|
|
1042
|
+
s = s.replace(/:[\w-]+(?:\([^)]*\))?/g, () => { b++; return '' })
|
|
1043
|
+
s.replace(/[^\s>+~,*|()&]+/g, () => { c++; return '' })
|
|
1044
|
+
return [a, b, c]
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
function rankCmp(x, y) {
|
|
1048
|
+
for (let i = 0; i < x.length; i++) if (x[i] !== y[i]) return x[i] - y[i]
|
|
1049
|
+
return 0
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
const SIZE_PROPS = ['width', 'height', 'min-width', 'min-height', 'inline-size', 'block-size', 'min-inline-size', 'min-block-size', 'aspect-ratio', 'font-size']
|
|
1053
|
+
const LOGICAL = { 'inline-size': 'width', 'block-size': 'height', 'min-inline-size': 'min-width', 'min-block-size': 'min-height' } // 横排时逻辑尺寸就是宽高
|
|
1054
|
+
let declMemo = new Map() // 这一次测量里每个元素的声明:一个元素只扫一遍样式表
|
|
1055
|
+
let cssOpaque = false // 有读不了的布局样式表(别的源的字体表不算)
|
|
1056
|
+
|
|
1057
|
+
const noteOpaque = (sheet) => { if (!/font|typekit|icon/i.test(sheet.href || '')) cssOpaque = true }
|
|
1058
|
+
|
|
1059
|
+
/** 这个元素的几个尺寸属性最终用的是哪一条:!important、行内、@layer 的先后(!important 时反过来)、选择器优先级、靠后的,依次比 */
|
|
1060
|
+
function declaredOf(el) {
|
|
1061
|
+
if (declMemo.has(el)) return declMemo.get(el)
|
|
1062
|
+
const best = {}
|
|
1063
|
+
let seq = 0
|
|
1064
|
+
const layerRoots = new Map() // 层的树:每层记自己在同级里第几个出现(谁先出现谁优先级低)
|
|
1065
|
+
let anon = 0
|
|
1066
|
+
const layerOf = (name, up) => {
|
|
1067
|
+
let at = up
|
|
1068
|
+
for (const part of (name || `#${++anon}`).split('.')) {
|
|
1069
|
+
const sib = at ? at.kids : layerRoots
|
|
1070
|
+
if (!sib.has(part)) sib.set(part, { path: [...(at ? at.path : []), sib.size + 1], kids: new Map() })
|
|
1071
|
+
at = sib.get(part)
|
|
1072
|
+
}
|
|
1073
|
+
return at
|
|
1074
|
+
}
|
|
1075
|
+
const offer = (style, spec, layer, inline) => {
|
|
1076
|
+
for (let i = 0; i < style.length; i++) { // 按写的先后:同一条规则里后写的盖前写的(width 和 inline-size 互相盖)
|
|
1077
|
+
const name = style[i]
|
|
1078
|
+
if (!SIZE_PROPS.includes(name)) continue
|
|
1079
|
+
const value = style.getPropertyValue(name)
|
|
1080
|
+
const order = ++seq
|
|
1081
|
+
const prop = LOGICAL[name] || name
|
|
1082
|
+
const imp = style.getPropertyPriority(name) ? 1 : 0
|
|
1083
|
+
// 比层:普通声明直接写的压过子层、后出现的压过先出现的;!important 全反过来
|
|
1084
|
+
const lr = [0, 1, 2, 3, 4, 5, 6, 7].map((d) => { const n = layer && layer.path[d]; return n ? (imp ? -n : n) : (imp ? -1e6 : 1e6) })
|
|
1085
|
+
const rank = [imp, inline ? 1 : 0, ...lr, ...spec, order]
|
|
1086
|
+
if (!best[prop] || rankCmp(rank, best[prop].rank) > 0) best[prop] = { rank, value }
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
const on = (r) => {
|
|
1090
|
+
if (r.media && r.media.mediaText && !matchMedia(r.media.mediaText).matches) return false
|
|
1091
|
+
if (typeof CSSSupportsRule !== 'undefined' && r instanceof CSSSupportsRule && !CSS.supports(r.conditionText)) return false
|
|
1092
|
+
if (typeof CSSContainerRule !== 'undefined' && r instanceof CSSContainerRule) return false // 容器查询算不出来生不生效:页面最外层很少这么写,当不生效
|
|
1093
|
+
return true
|
|
1094
|
+
}
|
|
1095
|
+
const isLayer = (r) => typeof CSSLayerBlockRule !== 'undefined' && r instanceof CSSLayerBlockRule
|
|
1096
|
+
const isLayerList = (r) => typeof CSSLayerStatementRule !== 'undefined' && r instanceof CSSLayerStatementRule
|
|
1097
|
+
const scan = (rules, layer, parent) => {
|
|
1098
|
+
for (const r of rules) {
|
|
1099
|
+
if (isLayerList(r)) { for (const n of r.nameList) layerOf(n, layer); continue }
|
|
1100
|
+
if (r.styleSheet) { // @import
|
|
1101
|
+
if (!on(r)) continue
|
|
1102
|
+
let sub
|
|
1103
|
+
try { sub = r.styleSheet.cssRules } catch { noteOpaque(r.styleSheet); continue }
|
|
1104
|
+
scan(sub, r.layerName != null ? layerOf(r.layerName, layer) : layer, '')
|
|
1105
|
+
continue
|
|
1106
|
+
}
|
|
1107
|
+
if (r.cssRules && !r.selectorText) {
|
|
1108
|
+
if (on(r)) scan(r.cssRules, isLayer(r) ? layerOf(r.name, layer) : layer, parent)
|
|
1109
|
+
continue
|
|
1110
|
+
}
|
|
1111
|
+
if (!r.style || !r.selectorText) continue
|
|
1112
|
+
// 嵌套写法:子规则的选择器接在父规则后面
|
|
1113
|
+
const full = parent ? splitSelectors(r.selectorText).map((p) => (p.includes('&') ? p.replace(/&/g, `:is(${parent})`) : `:is(${parent}) ${p}`)).join(',') : r.selectorText
|
|
1114
|
+
if (SIZE_PROPS.some((p) => r.style.getPropertyValue(p))) {
|
|
1115
|
+
const parts = splitSelectors(full).filter((p) => { try { return el.matches(p) } catch { return false } })
|
|
1116
|
+
if (parts.length) offer(r.style, parts.map(specificity).reduce((x, y) => (rankCmp(x, y) >= 0 ? x : y)), layer, false)
|
|
1117
|
+
}
|
|
1118
|
+
if (r.cssRules && r.cssRules.length) scan(r.cssRules, layer, full)
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
for (const sheet of document.styleSheets) {
|
|
1122
|
+
if (sheet.disabled || (sheet.media && sheet.media.mediaText && !matchMedia(sheet.media.mediaText).matches)) continue
|
|
1123
|
+
let rules
|
|
1124
|
+
try { rules = sheet.cssRules } catch { noteOpaque(sheet); continue } // 别的源的样式表读不了
|
|
1125
|
+
scan(rules, null, '')
|
|
1126
|
+
}
|
|
1127
|
+
offer(el.style, [0, 0, 0], null, true)
|
|
1128
|
+
declMemo.set(el, best)
|
|
1129
|
+
return best
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
const FLUID_UNIT = /[\d.](?:[sdl]?v[whib]|[sdl]?vmin|[sdl]?vmax)\b/
|
|
1133
|
+
|
|
1134
|
+
/** 声明的值,CSS 变量换成实际的 */
|
|
1135
|
+
function declaredValue(el, prop) {
|
|
1136
|
+
const d = declaredOf(el)[prop]
|
|
1137
|
+
return String((d && d.value) || '').trim().replace(/var\(\s*(--[\w-]+)\s*(?:,[^)]*)?\)/g, (m, n) => getComputedStyle(el).getPropertyValue(n).trim() || m).toLowerCase()
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* 这个元素的宽 / 高是怎么写的:'fixed' 写死;'floor' 有写死的下限(clamp、max);'fluid' 跟着窗口或没写死的父级;
|
|
1142
|
+
* '' 没写;'unknown' 没写但有读不了的样式表,说不准
|
|
1143
|
+
*/
|
|
1144
|
+
function sizeKind(el, prop) {
|
|
1145
|
+
const v = declaredValue(el, prop)
|
|
1146
|
+
if (!v) return cssOpaque ? 'unknown' : ''
|
|
1147
|
+
if (/^(auto|fit-content|max-content|min-content|initial|unset|inherit|revert)$/.test(v)) return ''
|
|
1148
|
+
if (FLUID_UNIT.test(v)) return /^(?:clamp|max)\(\s*[\d.]+px\s*,/.test(v) ? 'floor' : 'fluid'
|
|
1149
|
+
if (/[\d.]rem\b/.test(v) && FLUID_UNIT.test(declaredValue(document.documentElement, 'font-size'))) return 'fluid'
|
|
1150
|
+
if (/[\d.]%/.test(v)) {
|
|
1151
|
+
const up = el.parentElement ? boxKind(el.parentElement, prop.replace('min-', '')) : ''
|
|
1152
|
+
return up === 'fixed' || up === 'floor' ? 'fixed' : up === 'minfluid' ? 'minfluid' : 'fluid'
|
|
1153
|
+
}
|
|
1154
|
+
return 'fixed'
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/** 宽 / 高连最小值一起看。'minfluid':跟窗口走,但有写死的最小值 */
|
|
1158
|
+
function boxKind(el, prop) {
|
|
1159
|
+
const m = sizeKind(el, `min-${prop}`)
|
|
1160
|
+
const hasMin = m === 'fixed' || m === 'floor'
|
|
1161
|
+
const k = sizeKind(el, prop)
|
|
1162
|
+
if (k === 'fluid' && hasMin) return 'minfluid'
|
|
1163
|
+
if ((!k || k === 'unknown') && hasMin) return m
|
|
1164
|
+
return k
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
/** 写死的最小宽 / 高是多少像素(百分比的沿着父级找) */
|
|
1168
|
+
function minPx(el, prop) {
|
|
1169
|
+
for (let e = el; e; e = e.parentElement) {
|
|
1170
|
+
const floor = /^(?:clamp|max)\(\s*([\d.]+)px/.exec(declaredValue(e, prop)) // clamp(810px,…)、max(810px,…) 的下限
|
|
1171
|
+
if (floor) return parseFloat(floor[1])
|
|
1172
|
+
const min = getComputedStyle(e)[prop === 'width' ? 'minWidth' : 'minHeight']
|
|
1173
|
+
if (/px$/.test(min) && parseFloat(min)) return parseFloat(min)
|
|
1174
|
+
if (!/%/.test(declaredValue(e, prop)) && !/%/.test(min)) return 0
|
|
1175
|
+
}
|
|
1176
|
+
return 0
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
/**
|
|
1180
|
+
* 最外层那块的实际大小 [宽, 高](幻灯片 / 海报常写死 1440×810,比框大的话画幅要跟它走)。
|
|
1181
|
+
* null:正好和框一样大、又不是写死的(自适应的);0:不是一整页(几页竖着堆、还有别的东西往外撑)
|
|
1182
|
+
*/
|
|
1183
|
+
function naturalSize() {
|
|
1184
|
+
const b = document.body
|
|
1185
|
+
if (!b) return null
|
|
1186
|
+
declMemo = new Map()
|
|
1187
|
+
cssOpaque = false
|
|
1188
|
+
const skip = (el) => el.hasAttribute('data-dsh-design') || /^(SCRIPT|STYLE|LINK|NOSCRIPT|TEMPLATE)$/.test(el.tagName)
|
|
1189
|
+
const kidsOf = (el) => [...el.children].filter((k) => !skip(k))
|
|
1190
|
+
let box = null
|
|
1191
|
+
let area = 0
|
|
1192
|
+
for (const el of [...kidsOf(b), b]) { // 一样大时算里面那块
|
|
1193
|
+
const r = el.getBoundingClientRect()
|
|
1194
|
+
if (r.width * r.height > area) { area = r.width * r.height; box = el }
|
|
1195
|
+
}
|
|
1196
|
+
if (!box) return null
|
|
1197
|
+
// body 自己没写大小、里面只有一块:量那一块(窗口比它宽时 body 面积更大,但 body 的大小是窗口给的)
|
|
1198
|
+
const only = kidsOf(b)
|
|
1199
|
+
if (box === b && only.length === 1 && !['fixed', 'floor', 'minfluid', 'unknown'].includes(boxKind(b, 'width')) && !['fixed', 'floor', 'minfluid', 'unknown'].includes(boxKind(b, 'height')) && only[0].getBoundingClientRect().height) box = only[0]
|
|
1200
|
+
const wk = boxKind(box, 'width')
|
|
1201
|
+
let hk = boxKind(box, 'height')
|
|
1202
|
+
if ((!hk || hk === 'unknown') && /\d/.test(declaredValue(box, 'aspect-ratio'))) { // 高度没写:比例也算定了高度
|
|
1203
|
+
if (wk === 'fixed') hk = 'fixed'
|
|
1204
|
+
else if (wk === 'fluid' || wk === 'minfluid') hk = 'fluid'
|
|
1205
|
+
}
|
|
1206
|
+
const solid = (k) => k === 'fixed' || k === 'floor' || k === 'minfluid'
|
|
1207
|
+
const kids = kidsOf(box).map((k) => k.getBoundingClientRect()).filter((r) => r.width && r.height)
|
|
1208
|
+
const ratio = kids.length ? kids[0].width / kids[0].height : 0
|
|
1209
|
+
// 一样大的几页竖着堆(一个文件里放了几张幻灯片 / 海报):外层高度写死的是一页里分上下块;每块太扁的也不像一页
|
|
1210
|
+
if (!solid(hk) && kids.length >= 2 && kids[0].height >= 300 && ratio >= 0.45 && ratio <= 2.4 && kids.every((r) => Math.abs(r.width - kids[0].width) < 2 && Math.abs(r.height - kids[0].height) < 2) && kids[1].top >= kids[0].bottom - 1) return 0
|
|
1211
|
+
let { left, top, right, bottom } = box.getBoundingClientRect()
|
|
1212
|
+
// body 的宽高没写死:是窗口或内容给的,按里面的内容算
|
|
1213
|
+
if (box === b && kids.length && !solid(wk) && wk !== 'unknown') { left = Math.min(...kids.map((r) => r.left)); right = Math.max(...kids.map((r) => r.right)) }
|
|
1214
|
+
if (box === b && kids.length && !solid(hk) && hk !== 'unknown') { top = Math.min(...kids.map((r) => r.top)); bottom = Math.max(...kids.map((r) => r.bottom)) }
|
|
1215
|
+
const size = (a, z, view, scroll) => (Math.abs(a + z - view) <= 2 ? z - a : Math.max(z + scroll, z - a)) // 居中的只算它自己,靠左的连左边的空一起
|
|
1216
|
+
let w = Math.ceil(size(left, right, innerWidth, scrollX))
|
|
1217
|
+
let h = Math.ceil(size(top, bottom, innerHeight, scrollY))
|
|
1218
|
+
if (box !== b && wk === 'fluid') w = innerWidth // 按窗口算的(100vw、110vh):框放大了它跟着长,不能照着它放大
|
|
1219
|
+
if (box !== b && hk === 'fluid') h = innerHeight
|
|
1220
|
+
// 跟窗口走但有最小值的:只认最小值,别照着放大后的自己再放大(height:110vh 加 min-height)
|
|
1221
|
+
const follows = (k) => k === 'minfluid' || k === 'floor'
|
|
1222
|
+
const mw = follows(wk) ? minPx(box, 'width') : 0
|
|
1223
|
+
const mh = follows(hk) ? minPx(box, 'height') : 0
|
|
1224
|
+
if (follows(wk) && mw) w = Math.ceil(Math.max(innerWidth, mw))
|
|
1225
|
+
if (follows(hk) && mh) h = Math.ceil(Math.max(innerHeight, mh))
|
|
1226
|
+
// 宽高是内容撑出来的、比框小:那不是版面的大小,按框算(写死的小页面才照实报)
|
|
1227
|
+
if (!solid(wk) && w < innerWidth) w = innerWidth
|
|
1228
|
+
if (!solid(hk) && h < innerHeight) h = innerHeight
|
|
1229
|
+
const pinned = (k, min, view) => k === 'fixed' || (k === 'minfluid' && min >= view - 1) // 大小不随窗口变
|
|
1230
|
+
if (Math.abs(w - innerWidth) <= 1 && Math.abs(h - innerHeight) <= 1) return pinned(wk, mw, innerWidth) && pinned(hk, mh, innerHeight) ? [w, h] : null // 写死的正好装下也报:改稿后右侧栏才拿得到新大小
|
|
1231
|
+
const de = document.scrollingElement || document.documentElement
|
|
1232
|
+
return de.scrollWidth > Math.max(innerWidth, right + scrollX) + 40 || de.scrollHeight > Math.max(innerHeight, bottom + scrollY) + 40 ? 0 : [w, h]
|
|
1233
|
+
}
|
|
1234
|
+
|
|
996
1235
|
function boot(seed) {
|
|
997
1236
|
if (booted) return
|
|
998
1237
|
booted = true
|
|
@@ -1008,6 +1247,7 @@
|
|
|
1008
1247
|
tokens: tokensOf(),
|
|
1009
1248
|
links: firstLinks(),
|
|
1010
1249
|
dark: hasDark(),
|
|
1250
|
+
natural: naturalSize(),
|
|
1011
1251
|
changes: pending ? { count: pending.count, whole: pending.whole } : null,
|
|
1012
1252
|
})
|
|
1013
1253
|
postTop()
|
|
@@ -1793,7 +2033,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
1793
2033
|
const scale = Math.min(3, Math.max(1, Number(opts.scale) || 2))
|
|
1794
2034
|
const de = document.documentElement
|
|
1795
2035
|
const W = Math.ceil(de.clientWidth || innerWidth)
|
|
1796
|
-
const H = Math.ceil(full ? Math.max(de.scrollHeight, document.body ? document.body.scrollHeight : 0, innerHeight) : innerHeight)
|
|
2036
|
+
const H = Math.ceil(full ? Math.min(Number(opts.maxH) || Infinity, Math.max(de.scrollHeight, document.body ? document.body.scrollHeight : 0, innerHeight)) : innerHeight) // maxH:指定高度,别先画一张超长的图
|
|
1797
2037
|
const box = makeSandbox()
|
|
1798
2038
|
const inline = makeInliner()
|
|
1799
2039
|
// 只截看得到的这一屏(不是整页):页面滚过、里面的滚动区滚过、有贴住的(sticky)元素,画成图时都得照现在的样子摆
|
|
@@ -1888,7 +2128,8 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
1888
2128
|
const withUrls = []
|
|
1889
2129
|
const def = box.defaults(el)
|
|
1890
2130
|
let st = styleText(cs, def, withUrls)
|
|
1891
|
-
if (el === de) st += 'overflow:hidden;'
|
|
2131
|
+
if (el === de) st += (full ? `height:${H}px;max-height:none;` : '') + 'overflow:hidden;' // 整页:根元素写了 height:100%、max-height 也要放得下整页
|
|
2132
|
+
if (full && el === document.body) st += 'max-height:none;'
|
|
1892
2133
|
// iframe / 视频换成了 div:默认的 300×150、行内排列也得写明(和 iframe 默认一样的样式上面省掉了)
|
|
1893
2134
|
if (tag === 'IFRAME' || tag === 'VIDEO' || tag === 'OBJECT' || tag === 'EMBED') {
|
|
1894
2135
|
st += `display:${cs.display === 'inline' ? 'inline-block' : cs.display};width:${cs.width};height:${cs.height};box-sizing:${cs.boxSizing};vertical-align:${cs.verticalAlign};overflow:hidden;`
|
|
@@ -2272,6 +2513,36 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
2272
2513
|
}
|
|
2273
2514
|
}
|
|
2274
2515
|
|
|
2516
|
+
// 导出前预检:这一页实际多大、有几个点了没反应的链接(href="#"、指向不存在的文件)
|
|
2517
|
+
async function onMeasure(d) {
|
|
2518
|
+
try {
|
|
2519
|
+
if (document.readyState !== 'complete') await new Promise((r) => { addEventListener('load', r, { once: true }); setTimeout(r, 1500) })
|
|
2520
|
+
const de = document.documentElement
|
|
2521
|
+
const b = document.body
|
|
2522
|
+
const dead = [] // 每个死链接在页面上的纵坐标:导出的范围以外的不用报
|
|
2523
|
+
for (const a of document.querySelectorAll('a[href="#"],a[href=""]')) {
|
|
2524
|
+
if ((host && host.contains(a)) || a.hasAttribute('onclick') || a.hasAttribute('data-href') || a.hasAttribute('data-to')) continue
|
|
2525
|
+
const r = a.getBoundingClientRect()
|
|
2526
|
+
const seen = !a.checkVisibility || a.checkVisibility({ opacityProperty: true, visibilityProperty: true })
|
|
2527
|
+
if (seen && r.width >= 2 && r.height >= 2 && r.left < (de.clientWidth || innerWidth) && r.right > 0) dead.push(r.top + scrollY)
|
|
2528
|
+
}
|
|
2529
|
+
const found = linksOf().filter((l) => l.x < (de.clientWidth || innerWidth))
|
|
2530
|
+
const paths = found.map((l) => l.path)
|
|
2531
|
+
const gone = new Map()
|
|
2532
|
+
await Promise.all([...new Set(paths)].map(async (p) => {
|
|
2533
|
+
try {
|
|
2534
|
+
const r = await fetch(encodeURI(p), { cache: 'no-store' })
|
|
2535
|
+
gone.set(p, r.status === 404)
|
|
2536
|
+
if (r.body) r.body.cancel().catch(() => {})
|
|
2537
|
+
} catch { /* 查不了就不算 */ }
|
|
2538
|
+
}))
|
|
2539
|
+
for (const l of found) if (gone.get(l.path)) dead.push(l.y + scrollY)
|
|
2540
|
+
post({ type: 'measured', id: d.id, sy: scrollY, w: de.clientWidth || innerWidth, h: innerHeight, pageW: Math.max(de.scrollWidth, b ? b.scrollWidth : 0), pageH: Math.max(de.scrollHeight, b ? b.scrollHeight : 0), natural: naturalSize(), dead })
|
|
2541
|
+
} catch (e) {
|
|
2542
|
+
post({ type: 'measured', id: d.id, error: String((e && e.message) || e) })
|
|
2543
|
+
}
|
|
2544
|
+
}
|
|
2545
|
+
|
|
2275
2546
|
// ── 设计体检:在画好的页面上实测常见毛病,标在页面上,报给右侧栏(给人看的文字由右侧栏按界面语言拼) ──
|
|
2276
2547
|
//
|
|
2277
2548
|
// hscroll 超出屏幕(会横向滚动 / 被切掉) · clipped 文字被截断 · overflow 文字超出框 · overlap 文字叠在一起
|
|
@@ -3974,6 +4245,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
3974
4245
|
else if (d.type === 'unpin') unpin()
|
|
3975
4246
|
else if (d.type === 'reload') selfReload()
|
|
3976
4247
|
else if (d.type === 'capture') onCapture(d)
|
|
4248
|
+
else if (d.type === 'measure') onMeasure(d)
|
|
3977
4249
|
else if (d.type === 'check') onCheck(d)
|
|
3978
4250
|
else if (d.type === 'checkClear') { clearMarks(); lastIssues = [] }
|
|
3979
4251
|
else if (d.type === 'checkFocus') focusIssue(Number(d.i))
|
|
@@ -4011,7 +4283,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
4011
4283
|
links: () => linksOf().map(({ path, text }) => ({ path, text })),
|
|
4012
4284
|
info: () => {
|
|
4013
4285
|
const meta = document.querySelector('meta[name="dsh-design"]')
|
|
4014
|
-
return { title: document.title, meta: meta ? String(meta.getAttribute('content') || '').slice(0, 80) : '', page: Math.max(se().scrollHeight, innerHeight), w: innerWidth, h: innerHeight, topDark: topDark() }
|
|
4286
|
+
return { title: document.title, meta: meta ? String(meta.getAttribute('content') || '').slice(0, 80) : '', page: Math.max(se().scrollHeight, innerHeight), w: innerWidth, h: innerHeight, topDark: topDark(), natural: naturalSize() }
|
|
4015
4287
|
},
|
|
4016
4288
|
/** 'end':所有能滚的地方都滚到底;数字 i:主要的那块滚到第 i 屏(相邻两屏留出顶栏、底栏的高度,栏后面的内容下一屏能看到) */
|
|
4017
4289
|
// 滚完等页面跟上再截图
|