react-phone-apps/scripts/generate-maps.js
lanrtop 4e1d92264d feat: 初始化 Property Survey 应用基础架构
- 添加 monorepo 结构 (apps/property-survey, packages/types)
- 配置 Expo + React Native + Android 构建链
- 实现本地数据层 (SQLite/better-sqlite3)
- 添加 Blueprint 项目蓝图系统
- 配置 ESLint + Prettier
- 添加共享类型定义
2026-04-09 13:04:36 +09:00

79 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 将 img/ 目录下的 SVG 底图转换为 TS 模块
// 使用方式pnpm generate-maps
const fs = require('fs')
const path = require('path')
const imgDir = path.resolve(__dirname, '../apps/property-survey/img')
const outputFile = path.resolve(
__dirname,
'../apps/property-survey/src/features/map/floorMaps.generated.ts',
)
// 按楼层查找候选文件名(优先带 -1 后缀的新版本)
const FLOOR_CANDIDATES = {
1: ['1101-1.svg', '1101.svg'],
2: ['1102-1.svg', '1102.svg'],
3: ['1103-1.svg', '1103.svg'],
}
function findFile(candidates) {
for (const name of candidates) {
const full = path.join(imgDir, name)
if (fs.existsSync(full)) return full
}
return null
}
const contents = {}
for (const [floor, candidates] of Object.entries(FLOOR_CANDIDATES)) {
const filePath = findFile(candidates)
if (!filePath) {
console.error(`❌ 找不到 ${floor}F 的 SVG 文件(查找:${candidates.join(', ')}`)
process.exit(1)
}
console.log(`读取 ${floor}F → ${path.basename(filePath)}`)
let svg = fs.readFileSync(filePath, 'utf8')
// 提取原始 width / height用于补全 viewBox
const wMatch = svg.match(/<svg[^>]*\swidth="([^"]+)"/)
const hMatch = svg.match(/<svg[^>]*\sheight="([^"]+)"/)
const svgW = wMatch ? wMatch[1] : null
const svgH = hMatch ? hMatch[1] : null
// 若 viewBox 缺失,用原始尺寸补充;已有则保留
const hasViewBox = /viewBox=/.test(svg)
if (!hasViewBox && svgW && svgH) {
svg = svg.replace(/<svg/, `<svg viewBox="0 0 ${svgW} ${svgH}"`)
console.log(` → 补充 viewBox="0 0 ${svgW} ${svgH}"`)
}
// 将根 <svg> 的 width/height 改为 100%,让 SvgXml 的 props 完全控制渲染尺寸
svg = svg.replace(/(<svg[^>]*)\swidth="[^"]*"/, '$1 width="100%"')
svg = svg.replace(/(<svg[^>]*)\sheight="[^"]*"/, '$1 height="100%"')
// 将 xlink:href 替换为 href兼容 react-native-svg 的 <image> 渲染
svg = svg.replace(/xlink:href/g, 'href')
// 清除 base64 数据中的 XML 换行实体(&#10; &#13;和空白react-native-svg 不支持
svg = svg.replace(/href="(data:[^"]+)"/g, (_, dataUrl) => {
const cleaned = dataUrl.replace(/&#10;|&#13;|\s/g, '')
return `href="${cleaned}"`
})
contents[floor] = svg
}
const output = `// 自动生成,请勿手动编辑
// 更新底图后运行pnpm generate-maps
import type { FloorNumber } from '@repo/types'
const f1 = ${JSON.stringify(contents[1])}
const f2 = ${JSON.stringify(contents[2])}
const f3 = ${JSON.stringify(contents[3])}
export const FLOOR_SVG: Record<FloorNumber, string> = { 1: f1, 2: f2, 3: f3 }
`
fs.writeFileSync(outputFile, output, 'utf8')
console.log(`✅ 已生成 ${outputFile}`)