Add image buttons and exclusive launcher actions
This commit is contained in:
1 parent
8d604a5807
commit
74c725e8d7
23 files changed
+871
-107
No files matched your search
+163
-12
@@ -11,8 +11,8 @@ import ReleaseDialog from "./ReleaseDialog.vue";
|
||||
import LayoutSurface from "./LayoutSurface.vue";
|
||||
import DevicePreview from "./DevicePreview.vue";
|
||||
import {
|
||||
alignmentGuide, arrangeIconRow, builtInLayout, clamp, copyLayout, isIcon, normalizeLayout, resizedIconBounds, templates, validate,
|
||||
type Layout, type LayoutItem, type Payload, type Template,
|
||||
alignmentGuide, arrangeIconRow, builtInLayout, clamp, copyLayout, extraTypes, iconStyles, isIcon, itemLaunchMode, launchModes, normalizeLayout, resizedIconBounds, settingsActions, templates, validate,
|
||||
type LaunchMode, type Layout, type LayoutItem, type Template,
|
||||
} from "./layout";
|
||||
|
||||
const draftKey = "vibe-launcher-draft-v3";
|
||||
@@ -198,11 +198,80 @@ const backgrounds = [
|
||||
{ value: "solid", label: "纯色" },
|
||||
{ value: "gradient", label: "渐变" },
|
||||
];
|
||||
const payloadFields: { key: keyof Pick<Payload, "label" | "package" | "provider" | "intent">; label: string; type?: string }[] = [
|
||||
{ key: "label", label: "显示名称" }, { key: "package", label: "应用包名", type: "app_icon" },
|
||||
{ key: "provider", label: "组件 Provider", type: "widget" }, { key: "intent", label: "快捷方式 Intent", type: "shortcut" },
|
||||
];
|
||||
const visiblePayload = computed(() => payloadFields.filter(field => !field.type || field.type === selected.value?.type));
|
||||
const activeLaunchMode = computed(() => selected.value ? itemLaunchMode(selected.value) : "none");
|
||||
const supportsLaunchMode = computed(() => selected.value &&
|
||||
["app_icon", "image", "shortcut"].includes(selected.value.type));
|
||||
|
||||
function selectLaunchMode(item: LayoutItem, mode: LaunchMode) {
|
||||
if (itemLaunchMode(item) === mode) return;
|
||||
remember();
|
||||
item.payload.launchMode = mode;
|
||||
if (mode !== "settings") delete item.payload.settingsAction;
|
||||
if (mode !== "intent") delete item.payload.intent;
|
||||
if (mode !== "component") delete item.payload.componentName;
|
||||
if (mode !== "application") {
|
||||
delete item.payload.className;
|
||||
if (item.type !== "app_icon") delete item.payload.package;
|
||||
}
|
||||
}
|
||||
|
||||
function addIntentExtra(item: LayoutItem) {
|
||||
const extras = item.payload.extras ?? [];
|
||||
if (extras.length >= 32) return;
|
||||
updateValue(item.payload, "extras", [...extras, { key: "", type: "string", value: "" }]);
|
||||
}
|
||||
|
||||
const uploadingImage = ref(false);
|
||||
|
||||
async function uploadItemImage(event: Event, item: LayoutItem) {
|
||||
const input = event.target as HTMLInputElement;
|
||||
const file = input.files?.[0];
|
||||
input.value = "";
|
||||
if (!file || uploadingImage.value) return;
|
||||
uploadingImage.value = true;
|
||||
try {
|
||||
let upload: Blob = file;
|
||||
if (file.type === "image/webp") {
|
||||
const bitmap = await createImageBitmap(file);
|
||||
try {
|
||||
if (bitmap.width > 1024 || bitmap.height > 1024) throw new Error("图片宽高不能超过 1024 像素");
|
||||
const surface = document.createElement("canvas");
|
||||
surface.width = bitmap.width;
|
||||
surface.height = bitmap.height;
|
||||
surface.getContext("2d")!.drawImage(bitmap, 0, 0);
|
||||
upload = await new Promise<Blob>((resolve, reject) => surface.toBlob(
|
||||
blob => blob ? resolve(blob) : reject(new Error("无法转换 WebP 图片")), "image/png",
|
||||
));
|
||||
} finally {
|
||||
bitmap.close();
|
||||
}
|
||||
}
|
||||
if (upload.size > 512 * 1024) throw new Error("图片不能超过 512 KiB");
|
||||
const response = await fetch(new URL("./api/admin/images", location.href), {
|
||||
method: "POST", body: upload, credentials: "same-origin", redirect: "error",
|
||||
headers: { "X-Launcher-Request": "1" }, signal: AbortSignal.timeout(10000),
|
||||
});
|
||||
const result = await response.json();
|
||||
if (!response.ok) throw new Error(result.message || "图片上传失败");
|
||||
if (!layout.value.pages.some(entry => entry.items.includes(item))) return;
|
||||
remember();
|
||||
item.payload.imageId = result.id;
|
||||
if (item.type === "app_icon") item.payload.iconStyle = "image";
|
||||
ElMessage.success("已上传图片");
|
||||
} catch (error) {
|
||||
ElMessage.error(error instanceof Error ? error.message : "图片上传失败");
|
||||
} finally {
|
||||
uploadingImage.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function setItemIconStyle(item: LayoutItem, style: string) {
|
||||
if (item.payload.iconStyle === style || (style === "default" && !item.payload.iconStyle)) return;
|
||||
remember();
|
||||
if (style === "default") delete item.payload.iconStyle;
|
||||
else item.payload.iconStyle = style;
|
||||
}
|
||||
|
||||
const fields = ["x", "y", "w", "h", "z"] as const;
|
||||
|
||||
function remember() {
|
||||
@@ -508,7 +577,7 @@ onUnmounted(() => {
|
||||
<section>
|
||||
<h2>添加内容</h2>
|
||||
<div class="templates">
|
||||
<ElButton v-for="template in templates" :key="template.label" :disabled="page.items.length >= 80"
|
||||
<ElButton v-for="template in templates.filter(entry => hosted || entry.type !== 'image')" :key="template.label" :disabled="page.items.length >= 80"
|
||||
@click="addItem(template)">{{ template.label }}</ElButton>
|
||||
<ElButton v-if="hosted" :disabled="page.items.length >= 80"
|
||||
@click="widgetCatalog = true; if (loggedIn && !widgetProviders.length) readWidgetProviders()">
|
||||
@@ -517,7 +586,13 @@ onUnmounted(() => {
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<h2>图标大小</h2>
|
||||
<h2>图标样式与大小</h2>
|
||||
<label>默认图标样式
|
||||
<ElSelect :model-value="layout.iconStyle ?? 'system'" aria-label="默认图标样式"
|
||||
@change="updateValue(layout, 'iconStyle', $event)">
|
||||
<ElOption v-for="style in iconStyles" :key="style.value" v-bind="style" />
|
||||
</ElSelect>
|
||||
</label>
|
||||
<ElCheckbox v-model="iconSize.enabled">统一图标大小</ElCheckbox>
|
||||
<label>宽度 <ElInputNumber :model-value="effectiveIconSize.w" :min="1" :max="canvas.designWidth"
|
||||
:precision="0" aria-label="统一图标宽度" @change="value => { if (value !== undefined) iconSize.width = value; }" /></label>
|
||||
@@ -604,10 +679,86 @@ onUnmounted(() => {
|
||||
<ElInputNumber :model-value="selected[field]" :precision="field === 'x' ? undefined : 0" :aria-label="field"
|
||||
@change="updateValue(selected!, field, $event)" />
|
||||
</label>
|
||||
<label v-for="field in visiblePayload" :key="field.key">{{ field.label }}
|
||||
<input class="property-input" :value="selected.payload[field.key] ?? ''"
|
||||
@change="updateValue(selected!.payload, field.key, ($event.target as HTMLInputElement).value)">
|
||||
<label v-if="selected.type === 'app_icon'">图标样式
|
||||
<ElSelect :model-value="selected.payload.iconStyle ?? 'default'" aria-label="图标样式"
|
||||
@change="setItemIconStyle(selected!, $event)">
|
||||
<ElOption label="使用默认样式" value="default" />
|
||||
<ElOption v-for="style in iconStyles" :key="style.value" v-bind="style" />
|
||||
<ElOption label="上传图片" value="image" />
|
||||
</ElSelect>
|
||||
</label>
|
||||
<label v-if="hosted && (selected.type === 'image' || selected.type === 'app_icon' && selected.payload.iconStyle === 'image')">
|
||||
上传图片
|
||||
<input type="file" accept="image/png,image/jpeg,image/webp" :disabled="!loggedIn || uploadingImage"
|
||||
@change="uploadItemImage($event, selected!)">
|
||||
<small v-if="!loggedIn">请先登录后上传图片。</small>
|
||||
</label>
|
||||
<label>显示名称
|
||||
<input class="property-input" :value="selected.payload.label ?? ''"
|
||||
@change="updateValue(selected!.payload, 'label', ($event.target as HTMLInputElement).value)">
|
||||
</label>
|
||||
<label v-if="selected.type === 'widget'">组件 Provider
|
||||
<input class="property-input" :value="selected.payload.provider ?? ''"
|
||||
@change="updateValue(selected!.payload, 'provider', ($event.target as HTMLInputElement).value)">
|
||||
</label>
|
||||
<label v-if="supportsLaunchMode">点击动作
|
||||
<ElSelect :model-value="activeLaunchMode" aria-label="点击动作" @change="selectLaunchMode(selected!, $event)">
|
||||
<ElOption v-for="mode in launchModes.filter(mode => mode.value !== 'none' || selected!.type !== 'app_icon')"
|
||||
:key="mode.value" v-bind="mode" />
|
||||
</ElSelect>
|
||||
</label>
|
||||
<label v-if="supportsLaunchMode && activeLaunchMode === 'settings'">系统设置动作
|
||||
<ElSelect :model-value="selected.payload.settingsAction" aria-label="系统设置动作"
|
||||
@change="updateValue(selected!.payload, 'settingsAction', $event)">
|
||||
<ElOption v-for="action in settingsActions" :key="action.value"
|
||||
:label="action.label + ' · ' + action.value" :value="action.value" />
|
||||
</ElSelect>
|
||||
</label>
|
||||
<template v-if="supportsLaunchMode && activeLaunchMode === 'application'">
|
||||
<label>应用包名
|
||||
<input class="property-input" :value="selected.payload.package ?? ''"
|
||||
@change="updateValue(selected!.payload, 'package', ($event.target as HTMLInputElement).value)">
|
||||
</label>
|
||||
<label>Activity 类名
|
||||
<input class="property-input" :value="selected.payload.className ?? ''" placeholder="留空打开应用默认页"
|
||||
@change="updateValue(selected!.payload, 'className', ($event.target as HTMLInputElement).value)">
|
||||
</label>
|
||||
</template>
|
||||
<label v-if="supportsLaunchMode && activeLaunchMode === 'intent'">快捷方式 Intent
|
||||
<input class="property-input" :value="selected.payload.intent ?? ''"
|
||||
@change="updateValue(selected!.payload, 'intent', ($event.target as HTMLInputElement).value)">
|
||||
</label>
|
||||
<label v-if="supportsLaunchMode && activeLaunchMode === 'component'">ComponentName
|
||||
<input class="property-input" :value="selected.payload.componentName ?? ''" placeholder="com.example.app/.MainActivity"
|
||||
@change="updateValue(selected!.payload, 'componentName', ($event.target as HTMLInputElement).value)">
|
||||
</label>
|
||||
<label v-if="selected.type === 'app_icon' && activeLaunchMode !== 'application'">图标来源包名
|
||||
<input class="property-input" :value="selected.payload.package ?? ''"
|
||||
@change="updateValue(selected!.payload, 'package', ($event.target as HTMLInputElement).value)">
|
||||
</label>
|
||||
<section v-if="supportsLaunchMode && activeLaunchMode !== 'none'" class="intent-extras">
|
||||
<h2>Intent 附加参数</h2>
|
||||
<div v-for="(extra, index) in selected.payload.extras" :key="index" class="intent-extra">
|
||||
<ElInput :model-value="extra.key" placeholder="键名" aria-label="参数键名"
|
||||
@change="updateValue(extra, 'key', $event)" />
|
||||
<ElSelect :model-value="extra.type" aria-label="参数类型" @change="updateValue(extra, 'type', $event)">
|
||||
<ElOption v-for="type in extraTypes" :key="type" :label="type" :value="type" />
|
||||
</ElSelect>
|
||||
<ElSelect v-if="extra.type === 'boolean'" :model-value="extra.value" aria-label="参数值"
|
||||
@change="updateValue(extra, 'value', $event)">
|
||||
<ElOption label="true" value="true" />
|
||||
<ElOption label="false" value="false" />
|
||||
</ElSelect>
|
||||
<ElInput v-else :model-value="extra.value" placeholder="参数值" aria-label="参数值"
|
||||
@change="updateValue(extra, 'value', $event)" />
|
||||
<ElButton @click="updateValue(selected!.payload, 'extras', selected!.payload.extras?.filter((_, i) => i !== index))">
|
||||
删除参数
|
||||
</ElButton>
|
||||
</div>
|
||||
<ElButton :disabled="(selected.payload.extras?.length ?? 0) >= 32" @click="addIntentExtra(selected!)">
|
||||
添加参数
|
||||
</ElButton>
|
||||
</section>
|
||||
<div class="json-actions">
|
||||
<ElButton :disabled="selected.w > canvas.designWidth" @click="centerHorizontally">水平居中</ElButton>
|
||||
<ElButton @click="changeLayer(selected!, true)">置顶</ElButton>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
import {
|
||||
backgroundStyle, clamp, deviceMapper, devices, iconColor, sortedItems,
|
||||
backgroundStyle, clamp, deviceMapper, devices, iconColor, imageUrl, validImageId, sortedItems,
|
||||
type Layout, type LayoutItem, type Page,
|
||||
} from "./layout";
|
||||
|
||||
@@ -38,7 +38,7 @@ const whitespace = computed(() => {
|
||||
};
|
||||
});
|
||||
const items = computed(() => sortedItems(props.page));
|
||||
const known = new Set(["app_icon", "widget", "shortcut", "dock_bg"]);
|
||||
const known = new Set(["app_icon", "widget", "shortcut", "dock_bg", "image"]);
|
||||
const visible = computed(() => props.editable ? items.value : items.value.filter(item => known.has(item.type)));
|
||||
const percent = (n: number, total: number) => (n / total * 100) + "%";
|
||||
function itemStyle(item: LayoutItem) {
|
||||
@@ -69,8 +69,12 @@ function title(item: LayoutItem) {
|
||||
:class="[item.type, { selected: item.id === selectedId, 'row-selected': selectedRow?.includes(item.id) }]" :style="itemStyle(item)"
|
||||
:title="title(item)" :tabindex="editable ? 0 : undefined"
|
||||
@pointerdown="editable && emit('startDrag', $event, item, false)">
|
||||
<template v-if="item.type === 'app_icon' || item.type === 'shortcut'">
|
||||
<span class="icon-art">{{ (item.payload.label || title(item)).slice(0, 1).toUpperCase() }}</span>
|
||||
<img v-if="item.type === 'image' && validImageId(item.payload.imageId)" class="canvas-image"
|
||||
:src="imageUrl(item.payload.imageId)" :alt="title(item)" draggable="false">
|
||||
<template v-else-if="item.type === 'app_icon' || item.type === 'shortcut'">
|
||||
<img v-if="item.payload.iconStyle === 'image' && validImageId(item.payload.imageId)"
|
||||
class="icon-art uploaded-icon" :src="imageUrl(item.payload.imageId)" alt="" draggable="false">
|
||||
<span v-else class="icon-art" :class="'icon-' + (item.payload.iconStyle ?? layout.iconStyle ?? 'system')">{{ (item.payload.label || title(item)).slice(0, 1).toUpperCase() }}</span>
|
||||
<span class="icon-label">{{ title(item) }}</span>
|
||||
</template>
|
||||
<span v-else-if="item.type !== 'dock_bg'" class="widget-label">{{ title(item) }}</span>
|
||||
|
||||
+110
-1
@@ -1,11 +1,48 @@
|
||||
import defaultLayout from "../../android/app/src/main/assets/default_layout.json" with { type: "json" };
|
||||
|
||||
export interface IntentExtra {
|
||||
key: string;
|
||||
type: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export const settingsActions = [
|
||||
{ value: "ACTION_SETTINGS", label: "系统设置" },
|
||||
{ value: "ACTION_WIFI_SETTINGS", label: "Wi-Fi 设置" },
|
||||
{ value: "ACTION_BLUETOOTH_SETTINGS", label: "蓝牙设置" },
|
||||
{ value: "ACTION_WIRELESS_SETTINGS", label: "无线和网络设置" },
|
||||
{ value: "ACTION_AIRPLANE_MODE_SETTINGS", label: "飞行模式设置" },
|
||||
{ value: "ACTION_DISPLAY_SETTINGS", label: "显示设置" },
|
||||
{ value: "ACTION_SOUND_SETTINGS", label: "声音设置" },
|
||||
{ value: "ACTION_LOCATION_SOURCE_SETTINGS", label: "位置设置" },
|
||||
{ value: "ACTION_APPLICATION_SETTINGS", label: "应用设置" },
|
||||
{ value: "ACTION_DATE_SETTINGS", label: "日期和时间设置" },
|
||||
{ value: "ACTION_ACCESSIBILITY_SETTINGS", label: "无障碍设置" },
|
||||
];
|
||||
export const launchModes = [
|
||||
{ value: "none", label: "无动作" },
|
||||
{ value: "settings", label: "系统设置动作" },
|
||||
{ value: "application", label: "应用包名 / Activity 类名" },
|
||||
{ value: "intent", label: "快捷方式 Intent" },
|
||||
{ value: "component", label: "ComponentName" },
|
||||
] as const;
|
||||
export type LaunchMode = typeof launchModes[number]["value"];
|
||||
|
||||
export const extraTypes = ["string", "boolean", "int", "long", "double"];
|
||||
|
||||
export interface Payload {
|
||||
label?: string | null;
|
||||
package?: string | null;
|
||||
provider?: string | null;
|
||||
intent?: string | null;
|
||||
radius?: number | null;
|
||||
imageId?: string;
|
||||
className?: string;
|
||||
settingsAction?: string;
|
||||
launchMode?: LaunchMode;
|
||||
componentName?: string;
|
||||
extras?: IntentExtra[];
|
||||
iconStyle?: string;
|
||||
}
|
||||
export interface LayoutItem {
|
||||
id: string;
|
||||
@@ -17,6 +54,16 @@ export interface LayoutItem {
|
||||
z: number;
|
||||
payload: Payload;
|
||||
}
|
||||
export function itemLaunchMode(item: LayoutItem): LaunchMode {
|
||||
const payload = item.payload;
|
||||
return payload.launchMode ?? (item.type === "app_icon" ? "application"
|
||||
: payload.settingsAction ? "settings"
|
||||
: payload.componentName ? "component"
|
||||
: payload.className ? "application"
|
||||
: payload.intent ? "intent"
|
||||
: payload.package ? "application" : "none");
|
||||
}
|
||||
|
||||
export interface Background {
|
||||
type: string;
|
||||
color?: string;
|
||||
@@ -27,6 +74,7 @@ export interface Page { index: number; background: Background; items: LayoutItem
|
||||
export interface Layout {
|
||||
schemaVersion: 3;
|
||||
revision: number;
|
||||
iconStyle?: string;
|
||||
canvas: { designWidth: number; designHeight: number; safeTop: number; safeBottom: number };
|
||||
fit: { mode: string; anchorY: number };
|
||||
pages: Page[];
|
||||
@@ -34,6 +82,15 @@ export interface Layout {
|
||||
export interface Issue { level: "error" | "warning"; path: string; message: string }
|
||||
export type Template = Pick<LayoutItem, "type" | "w" | "h" | "payload"> & { label: string };
|
||||
|
||||
export const iconStyles = [
|
||||
{ value: "original", label: "应用原图" },
|
||||
{ value: "system", label: "跟随系统" },
|
||||
{ value: "circle", label: "圆形" },
|
||||
{ value: "rounded", label: "圆角方形" },
|
||||
];
|
||||
export const validImageId = (id: unknown): id is string => typeof id === "string" && /^[a-f0-9]{64}$/.test(id);
|
||||
export const imageUrl = (id: string) => new URL("./api/images/" + id, location.href).href;
|
||||
|
||||
export const templates: Template[] = [
|
||||
{ type: "app_icon", label: "应用图标", w: 132, h: 168, payload: { package: "com.tencent.mm", label: "微信" } },
|
||||
{ type: "app_icon", label: "相机", w: 132, h: 168, payload: { package: "com.android.camera2", label: "相机" } },
|
||||
@@ -41,6 +98,7 @@ export const templates: Template[] = [
|
||||
{ type: "widget", label: "搜索组件", w: 420, h: 300, payload: { provider: "com.android.quicksearchbox/com.android.quicksearchbox.SearchWidgetProvider", label: "搜索" } },
|
||||
{ type: "widget", label: "音乐组件", w: 420, h: 300, payload: { provider: "com.tencent.qqmusic/com.tencent.qqmusiccommon.WidgetProviderSmall", label: "音乐" } },
|
||||
{ type: "shortcut", label: "快捷方式", w: 132, h: 168, payload: { intent: "weixin://scanqrcode", label: "扫一扫" } },
|
||||
{ type: "image", label: "图片按钮", w: 300, h: 200, payload: { label: "图片" } },
|
||||
{ type: "dock_bg", label: "Dock 底板", w: 936, h: 200, payload: { radius: 56 } },
|
||||
];
|
||||
|
||||
@@ -73,6 +131,7 @@ export function normalizeLayout(raw: unknown): Layout {
|
||||
|| canvas.designHeight > 8192 || typeof fit.mode !== "string" || !finite(fit.anchorY)) {
|
||||
throw new Error("画布尺寸或映射参数无效");
|
||||
}
|
||||
if (raw.iconStyle !== undefined && !iconStyles.some(style => style.value === raw.iconStyle)) throw new Error("未知的默认图标样式");
|
||||
if (raw.revision !== undefined && !int(raw.revision)) throw new Error("版本号必须是整数");
|
||||
if (!Array.isArray(raw.pages) || !raw.pages.length || raw.pages.length > 20) throw new Error("布局必须包含 1–20 页");
|
||||
const pages = raw.pages.map((page, index) => {
|
||||
@@ -94,9 +153,15 @@ export function normalizeLayout(raw: unknown): Layout {
|
||||
throw new Error("元素 ID、类型、尺寸或层级格式无效");
|
||||
}
|
||||
const payload = item.payload === undefined ? {} : item.payload;
|
||||
if (!object(payload) || ["label", "package", "provider", "intent"].some(
|
||||
if (!object(payload) || ["label", "package", "provider", "intent", "imageId", "iconStyle", "className", "settingsAction", "componentName", "launchMode"].some(
|
||||
key => payload[key] != null && typeof payload[key] !== "string",
|
||||
) || payload.radius != null && !int(payload.radius)) throw new Error("元素内容格式无效");
|
||||
if (payload.launchMode !== undefined && !launchModes.some(mode => mode.value === payload.launchMode)) {
|
||||
throw new Error("未知的点击动作类型");
|
||||
}
|
||||
if (payload.extras !== undefined && (!Array.isArray(payload.extras) || payload.extras.length > 32 ||
|
||||
payload.extras.some(extra => !object(extra) || typeof extra.key !== "string" ||
|
||||
typeof extra.type !== "string" || typeof extra.value !== "string"))) throw new Error("Intent 参数格式无效");
|
||||
return { ...item, id: item.id, type: item.type, x: item.x, y: item.y, w: item.w, h: item.h,
|
||||
z: item.z ?? 1, payload };
|
||||
}),
|
||||
@@ -138,6 +203,36 @@ export function validate(layout: Layout): Issue[] {
|
||||
item.x + item.w > designWidth + 0.5 || item.y + item.h > designHeight + 0.5) {
|
||||
add("error", path, "尺寸无效或元素超出画布");
|
||||
}
|
||||
if (item.payload.imageId !== undefined && !validImageId(item.payload.imageId) ||
|
||||
(item.type === "image" || item.payload.iconStyle === "image") && !validImageId(item.payload.imageId)) {
|
||||
add("error", path, "请上传有效的图片");
|
||||
}
|
||||
if (item.payload.iconStyle !== undefined && item.payload.iconStyle !== "image" &&
|
||||
!iconStyles.some(style => style.value === item.payload.iconStyle)) add("error", path, "未知的图标样式");
|
||||
const mode = itemLaunchMode(item);
|
||||
const cls = item.payload.className?.trim();
|
||||
if (mode === "application" && (cls || item.payload.launchMode === "application") &&
|
||||
(!item.payload.package?.trim() || cls && !/^\.?[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*)*$/.test(cls))) {
|
||||
add("error", path, "Activity 类名格式无效,或缺少应用包名");
|
||||
}
|
||||
if (mode === "settings" && !settingsActions.some(action => action.value === item.payload.settingsAction)) {
|
||||
add("error", path, "请选择系统设置动作");
|
||||
}
|
||||
if (mode === "component" && !/^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\/\.?[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/
|
||||
.test(item.payload.componentName?.trim() ?? "")) {
|
||||
add("error", path, "ComponentName 应为包名/类名");
|
||||
}
|
||||
if (item.payload.launchMode === "intent" && !item.payload.intent?.trim()) {
|
||||
add("error", path, "请填写快捷方式 Intent");
|
||||
}
|
||||
const keys = new Set<string>();
|
||||
for (const extra of item.payload.extras ?? []) {
|
||||
const key = extra.key.trim();
|
||||
if (!key || key.length > 128 || extra.value.length > 2048 || keys.has(key) || !validExtra(extra)) {
|
||||
add("error", path, "Intent 参数键重复、为空或值与类型不符");
|
||||
}
|
||||
keys.add(key);
|
||||
}
|
||||
if (item.type === "app_icon" && !item.payload.package?.trim()) add("error", path, "应用图标需要包名");
|
||||
if (item.type === "widget" && !item.payload.provider?.includes("/")) add("error", path, "组件需填写包名/类名");
|
||||
if (item.type === "shortcut" && !item.payload.intent?.trim()) add("warning", path, "快捷方式缺少 Intent");
|
||||
@@ -163,6 +258,20 @@ interface DeviceMapping {
|
||||
gapScale?: number;
|
||||
}
|
||||
|
||||
function validExtra(extra: IntentExtra): boolean {
|
||||
const value = extra.value.trim();
|
||||
switch (extra.type) {
|
||||
case "string": return true;
|
||||
case "boolean": return value === "true" || value === "false";
|
||||
case "int": return /^[-+]?\d+$/.test(value) && int(Number(value));
|
||||
case "long":
|
||||
if (!/^[-+]?\d+$/.test(value)) return false;
|
||||
return BigInt(value) >= -9223372036854775808n && BigInt(value) <= 9223372036854775807n;
|
||||
case "double": return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[-+]?\d+)?$/i.test(value) && Number.isFinite(Number(value));
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function deviceMapper(canvas: Layout["canvas"], fit: Layout["fit"], items: LayoutItem[],
|
||||
dev: Pick<typeof devices[number], "w" | "h">): DeviceMapping {
|
||||
const sx = dev.w / canvas.designWidth;
|
||||
|
||||
@@ -146,3 +146,11 @@ pre { color: #a7ccea; font-size: 11px; max-width: 100%; overflow: auto; }
|
||||
.preview-swatch { width: 14px; height: 10px; border: 1px solid #f4bb66; border-radius: 2px; }
|
||||
.preview-legend, .whitespace-examples p { font-size: 12px; color: #b7c5d7; }
|
||||
.whitespace-examples { display: grid; gap: 9px; margin-top: 16px; padding-top: 12px; border-top: 1px solid #2a3547; }
|
||||
|
||||
.canvas-image { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
|
||||
.icon-art.uploaded-icon { object-fit: contain; border-radius: 0; background: none; }
|
||||
.icon-art.icon-circle { border-radius: 50%; }
|
||||
.icon-art.icon-original { border-radius: 0; }
|
||||
|
||||
.intent-extras { display: grid; gap: 8px; }
|
||||
.intent-extra { display: grid; grid-template-columns: minmax(0, 1fr) 80px; gap: 6px; }
|
||||
@@ -1,7 +1,7 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { runInNewContext } from "node:vm";
|
||||
import { alignmentGuide, arrangeIconRow, builtInLayout, deviceMapper, normalizeLayout, resizedIconBounds, validate } from "./src/layout.ts";
|
||||
import { alignmentGuide, arrangeIconRow, builtInLayout, deviceMapper, itemLaunchMode, normalizeLayout, resizedIconBounds, validate } from "./src/layout.ts";
|
||||
|
||||
const fixtures = JSON.parse(readFileSync(new URL("../docs/layout-cases.json", import.meta.url), "utf8"));
|
||||
const offline = readFileSync(new URL("./canvas.html", import.meta.url), "utf8");
|
||||
@@ -83,3 +83,24 @@ assert.deepEqual(resizedIconBounds(icon, icon.w + 16, icon.h + 20, canvas),
|
||||
assert.deepEqual(resizedIconBounds({ ...icon, x: 0, y: 0 }, 200, 220, canvas),
|
||||
{ x: 0, y: 0, w: 200, h: 220 });
|
||||
console.log("Icon resize centering and canvas bounds passed");
|
||||
|
||||
const settingsButton = normalizeLayout({ pages: [{ items: [{
|
||||
id: "wifi", type: "image", x: 0, y: 200, w: 100, h: 100,
|
||||
payload: { imageId: "a".repeat(64), settingsAction: "ACTION_WIFI_SETTINGS",
|
||||
extras: [{ key: "example", type: "long", value: "9223372036854775807" }] },
|
||||
}] }] });
|
||||
assert.equal(validate(settingsButton).filter(issue => issue.level === "error").length, 0);
|
||||
settingsButton.pages[0].items[0].payload.extras[0].value = "9223372036854775808";
|
||||
assert.ok(validate(settingsButton).some(issue => issue.level === "error" && issue.message.includes("Intent")));
|
||||
console.log("Settings actions and typed extras passed");
|
||||
|
||||
const picture = settingsButton.pages[0].items[0];
|
||||
picture.payload.extras = [];
|
||||
assert.equal(itemLaunchMode(picture), "settings");
|
||||
picture.payload.launchMode = "component";
|
||||
picture.payload.componentName = "com.example.app/.MainActivity";
|
||||
assert.equal(itemLaunchMode(picture), "component");
|
||||
assert.ok(!validate(settingsButton).some(issue => issue.level === "error"));
|
||||
picture.payload.componentName = "com.example.app";
|
||||
assert.ok(validate(settingsButton).some(issue => issue.message.includes("ComponentName")));
|
||||
console.log("Exclusive action mode and ComponentName validation passed");
|
||||
Reference in new issue
Block a user