fix(image-annotate): v1.8.3 图层不透明度滑杆可拖动、图层拖动排序真正生效

- 去掉图层项的 HTML5 draggable(它会把子元素的原生拖动一起接管,
  滑杆因此只能点不能拖)
- 排序改为指针拖动:越过行中线就实时插位,松手读 DOM 顺序落地,
  修掉「向下拖一格索引回退等于原地不动」
- 拖动中整行浮起、其余行让位;原图固定最底层;滑块拖动像素实时跟随
- blur 兜底必须判 e.target === window,否则子元素失焦会取消拖动
- harness 新增 6 项(真实鼠标拖滑杆 / 换位与 z / 拖到最底 /
  位移不足算点击 / 焦点在工具栏时也能拖 / 位图与矢量混排),共 146 项
This commit is contained in:
qiqi committed 2026-11-08 19:27:09 +08:00
1 parent 6e1685998f
commit 040b08fe28
7 files changed
+547 -65

No files matched your search

+152 -36
View File
@@ -9,7 +9,7 @@
var __IWB_ANNOTATE_MANIFEST__ = {
id: 'image-annotate',
name: '标注 / 修图',
version: '1.8.2',
version: '1.8.3',
apiVersion: 1,
capabilities: ['commands', 'nodeActions', 'canvas', 'canvasWrite'],
settings: {
@@ -2116,10 +2116,11 @@ var __IWB_ANNOTATE_MANIFEST__ = {
const delSvg = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>';
const pct = opacityPct(item.opacity);
const canRename = item.type === "bitmap";
html += `<div class="iwb-layer-item${activeClass}${multiClass}${visClass}" draggable="${item.id !== "base"}" data-layer-id="${item.id}" data-layer-type="${item.type}">
const sortHint = item.id === "base" ? "原图(固定在最底层)" : "拖动调整叠放顺序";
html += `<div class="iwb-layer-item${activeClass}${multiClass}${visClass}" data-layer-id="${item.id}" data-layer-type="${item.type}">
<div class="iwb-layer-row">
<button class="iwb-layer-vis" data-vis-id="${item.id}" data-vis-type="${item.type}" title="${item.visible ? "隐藏" : "显示"}">${visSvg}</button>
<span class="iwb-layer-thumb"><canvas width="44" height="44" data-thumb-id="${item.id}" data-thumb-type="${item.type}"></canvas><span class="iwb-layer-icon">${typeIconSvg}</span></span>
<span class="iwb-layer-thumb" title="${sortHint}"><canvas width="44" height="44" data-thumb-id="${item.id}" data-thumb-type="${item.type}"></canvas><span class="iwb-layer-icon">${typeIconSvg}</span></span>
<span class="iwb-layer-name${canRename ? " renamable" : ""}" data-name-id="${item.id}" data-name-type="${item.type}" title="${esc(item.name)}${canRename ? " · 双击重命名" : ""}">${esc(item.name)}</span>
${item.paintTarget ? '<span class="iwb-layer-paint" title="当前画笔 / 橡皮作用图层">✎</span>' : ""}
${pct < 100 && !item.active ? `<span class="iwb-layer-op" title="不透明度 ${pct}%(选中该层可调)">${pct}%</span>` : ""}
@@ -2142,33 +2143,154 @@ var __IWB_ANNOTATE_MANIFEST__ = {
}
bindLayerPanelEvents();
}
function reorderPanelItem(dragId, targetId) {
if (!dragId || !targetId || dragId === targetId || dragId === "base" || targetId === "base") return;
const all = layerUnits();
const baseIdx = all.findIndex((u) => u.obj.id === "base");
if (baseIdx > 0) {
const [b] = all.splice(baseIdx, 1);
all.unshift(b);
function planPanelOrder(descKeys) {
const units = layerUnits();
const byKey = new Map(units.map((u) => [u.kind + ":" + u.obj.id, u]));
const ordered = [];
for (const key of descKeys.slice().reverse()) {
const u = byKey.get(key);
if (!u) continue;
byKey.delete(key);
ordered.push(u);
}
const from = all.findIndex((item) => item.obj.id === dragId);
const to0 = all.findIndex((item) => item.obj.id === targetId);
if (from < 0 || to0 < 0) return;
const before = all.map((u) => u.obj.id).join("|");
const [moved] = all.splice(from, 1);
const to = Math.max(all.findIndex((item) => item.obj.id === targetId), baseIdx >= 0 ? 1 : 0);
all.splice(to, 0, moved);
if (all.map((u) => u.obj.id).join("|") === before) return;
saveSnapshot();
assignLayerZ(all);
if (!ordered.length || byKey.size) return null;
const baseIdx = ordered.findIndex((u) => u.obj.id === "base");
if (baseIdx > 0) {
const [b] = ordered.splice(baseIdx, 1);
ordered.unshift(b);
}
const before = units.map((u) => u.obj.id).join("|");
return { ordered, changed: before !== ordered.map((u) => u.obj.id).join("|") };
}
function applyPanelOrder(descKeys) {
const plan = planPanelOrder(descKeys);
if (!plan || !plan.changed) {
renderLayerPanel();
return false;
}
saveSnapshot("调整图层顺序");
assignLayerZ(plan.ordered);
reorderLayerCanvas();
sortLayersByZ();
renderObjects();
renderLayerPanel();
return true;
}
var SORT_THRESHOLD = 4;
var _sortDrag = null;
function bindLayerDragSort(list) {
if (list._iwbSortBound) return;
list._iwbSortBound = true;
list.addEventListener("pointerdown", (e) => {
if (e.button !== 0 || _sortDrag) return;
const item = e.target.closest(".iwb-layer-item");
if (!item || !list.contains(item)) return;
if (item.dataset.layerId === "base") return;
if (e.target.closest(".iwb-layer-op-row, input, button, .iwb-layer-name-input")) return;
_sortDrag = {
list,
item,
id: item.dataset.layerId,
type: item.dataset.layerType,
startY: e.clientY,
y: e.clientY,
pointerId: e.pointerId,
active: false
};
document.addEventListener("pointermove", onSortMove, true);
document.addEventListener("pointerup", onSortEnd, true);
document.addEventListener("pointercancel", onSortCancel, true);
window.addEventListener("blur", onWindowBlur, true);
});
}
function onSortMove(e) {
const d = _sortDrag;
if (!d) return;
if (e.pointerId != null && d.pointerId != null && e.pointerId !== d.pointerId) return;
d.y = e.clientY;
if (!d.active) {
if (Math.abs(e.clientY - d.startY) < SORT_THRESHOLD) return;
const rect = d.item.getBoundingClientRect();
d.grabY = e.clientY - rect.top;
d.active = true;
d.list.classList.add("sorting");
d.item.classList.add("dragging");
S.lastNameClick = null;
}
updateSortTarget(d);
}
function updateSortTarget(d) {
const list = d.list;
const lr = list.getBoundingClientRect();
if (d.y < lr.top + 18) list.scrollTop -= 8;
else if (d.y > lr.bottom - 18) list.scrollTop += 8;
const rows = Array.from(list.querySelectorAll(".iwb-layer-item"));
const baseEl = rows.find((el) => el.dataset.layerId === "base") || null;
const movable = rows.filter((el) => el !== baseEl && el !== d.item);
let anchor = baseEl || null;
for (const el of movable) {
const r2 = el.getBoundingClientRect();
if (d.y < r2.top + r2.height / 2) {
anchor = el;
break;
}
}
if (d.item.nextElementSibling !== anchor || d.item.parentNode !== list) {
if (anchor) list.insertBefore(d.item, anchor);
else list.appendChild(d.item);
}
d.item.style.transform = "";
const r = d.item.getBoundingClientRect();
const dy = Math.round(d.y - d.grabY - r.top);
d.item.style.transform = dy ? "translateY(" + dy + "px)" : "";
}
function onSortEnd(e) {
const d = _sortDrag;
if (!d) return;
if (e && e.pointerId != null && d.pointerId != null && e.pointerId !== d.pointerId) return;
cleanupSortDrag();
if (!d.active) return;
const rows = Array.from(d.list.querySelectorAll(".iwb-layer-item"));
const keyOf = (el) => el.dataset.layerType + ":" + el.dataset.layerId;
const baseEl = rows.find((el) => el.dataset.layerId === "base") || null;
const desc = rows.filter((el) => el !== baseEl).map(keyOf);
if (baseEl) desc.push(keyOf(baseEl));
if (d.type === "bitmap") {
if (S.layers.some((l) => l.id === d.id)) S.selectedLayerId = d.id;
} else if (d.type === "text" || d.type === "shape") {
selectUnit(d.type, d.id);
}
applyPanelOrder(desc);
}
function onSortCancel(e) {
const d = _sortDrag;
if (!d) return;
if (e && e.pointerId != null && d.pointerId != null && e.pointerId !== d.pointerId) return;
const wasActive = d.active;
cleanupSortDrag();
if (wasActive) renderLayerPanel();
}
function onWindowBlur(e) {
if (e.target !== window && e.target !== document) return;
onSortCancel(null);
}
function cleanupSortDrag() {
const d = _sortDrag;
if (!d) return;
document.removeEventListener("pointermove", onSortMove, true);
document.removeEventListener("pointerup", onSortEnd, true);
document.removeEventListener("pointercancel", onSortCancel, true);
window.removeEventListener("blur", onWindowBlur, true);
_sortDrag = null;
d.list.classList.remove("sorting");
d.item.classList.remove("dragging");
d.item.style.transform = "";
}
function bindLayerPanelEvents() {
const list = runtime.el.querySelector(".iwb-editor-layer-list");
if (!list) return;
bindLayerItemEvents(list);
bindLayerDragSort(list);
bindLayerNameRename(list);
bindLayerOpacityInputs(list);
bindLayerVisButtons(list);
@@ -2178,20 +2300,6 @@ var __IWB_ANNOTATE_MANIFEST__ = {
function bindLayerItemEvents(list) {
list.querySelectorAll(".iwb-layer-item").forEach((item) => {
item.addEventListener("mousedown", (e) => e.stopPropagation());
item.addEventListener("dragstart", (e) => {
if (item.draggable) {
e.dataTransfer.setData("text/plain", item.dataset.layerId);
item.classList.add("dragging");
}
});
item.addEventListener("dragend", () => item.classList.remove("dragging"));
item.addEventListener("dragover", (e) => {
if (item.draggable) e.preventDefault();
});
item.addEventListener("drop", (e) => {
e.preventDefault();
reorderPanelItem(e.dataTransfer.getData("text/plain"), item.dataset.layerId);
});
item.addEventListener("click", (e) => {
if (e.target.closest(".iwb-layer-vis") || e.target.closest(".iwb-layer-lock") || e.target.closest(".iwb-layer-del")) return;
if (e.target.closest(".iwb-layer-opacity") || e.target.closest(".iwb-layer-name-input") || e.target.closest(".iwb-layer-op-row")) return;
@@ -2243,7 +2351,6 @@ var __IWB_ANNOTATE_MANIFEST__ = {
input.value = layer.name;
input.maxLength = 40;
span.replaceWith(input);
item.draggable = false;
let done = false;
const finish = (commit) => {
if (done) return;
@@ -2277,13 +2384,22 @@ var __IWB_ANNOTATE_MANIFEST__ = {
input.addEventListener("click", (e) => e.stopPropagation());
input.addEventListener("dblclick", (e) => {
e.stopPropagation();
input._opSnapTaken = false;
input.value = "100";
applyOpacityFromInput(input);
input._opSnapTaken = false;
});
input.addEventListener("input", (e) => {
e.stopPropagation();
applyOpacityFromInput(input);
});
const resetSnap = () => {
input._opSnapTaken = false;
};
input.addEventListener("change", resetSnap);
input.addEventListener("pointerup", resetSnap);
input.addEventListener("pointercancel", resetSnap);
input.addEventListener("blur", resetSnap);
});
}
function applyOpacityFromInput(input) {
@@ -7163,7 +7279,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
};
// image-annotate/plugin/editor.css
var editor_default = '/* IWB 鲜艺抠图节点 - 注入样式 */\n\n/* 抠图按钮 */\n.iwb-cutout-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n\n.iwb-cutout-btn:hover {\n opacity: 1;\n transform: scale(1.15);\n}\n\n.iwb-cutout-btn svg {\n width: 14px;\n height: 14px;\n display: block;\n}\n\n/* 正在处理中的按钮状态 */\n.iwb-cutout-btn.processing {\n animation: iwb-cutout-pulse 1s infinite;\n pointer-events: none;\n}\n\n@keyframes iwb-cutout-pulse {\n 0%, 100% { opacity: 0.4; }\n 50% { opacity: 1; }\n}\n\n/* 全屏遮罩 */\n.iwb-cutout-overlay {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.25);\n display: none;\n align-items: center;\n justify-content: center;\n flex-direction: column;\n gap: 12px;\n z-index: 999999;\n pointer-events: all;\n backdrop-filter: blur(2px);\n}\n\n/* 自定义按钮提示(挂在 body,最高层级,不会被图片遮挡) */\n#iwb-cutout-tip {\n position: fixed;\n z-index: 2147483647;\n background: rgba(30, 30, 46, 0.95);\n color: #fff;\n padding: 5px 10px;\n border-radius: 6px;\n font-size: 12px;\n line-height: 1.5;\n white-space: nowrap;\n pointer-events: none;\n display: none;\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);\n border: 1px solid rgba(255, 255, 255, 0.14);\n font-family: system-ui, -apple-system, "Segoe UI", sans-serif;\n max-width: 320px;\n white-space: normal;\n word-break: break-all;\n}\n\n/* 加载动画 */\n.iwb-cutout-spinner {\n width: 48px;\n height: 48px;\n border: 4px solid rgba(255, 255, 255, 0.3);\n border-top-color: #e8a735;\n border-radius: 50%;\n animation: iwb-cutout-spin 0.8s linear infinite;\n}\n\n@keyframes iwb-cutout-spin {\n to { transform: rotate(360deg); }\n}\n\n/* 遮罩文字 */\n.iwb-cutout-text {\n color: #fff;\n font-size: 14px;\n font-weight: 500;\n text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);\n background: rgba(0, 0, 0, 0.6);\n padding: 8px 16px;\n border-radius: 6px;\n}\n\n/* Toast 提示 */\n.iwb-cutout-toast {\n position: fixed;\n bottom: 24px;\n left: 50%;\n transform: translateX(-50%);\n padding: 10px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-weight: 500;\n z-index: 999999;\n transition: opacity 0.3s;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);\n display: none;\n max-width: 400px;\n}\n\n.iwb-cutout-toast-success {\n background: #1a7f37;\n color: #fff;\n}\n\n.iwb-cutout-toast-error {\n background: #cf222e;\n color: #fff;\n}\n\n.iwb-cutout-toast-info {\n background: #1f6feb;\n color: #fff;\n}\n\n/* 白底按钮 */\n.iwb-whitebg-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-whitebg-btn:hover { opacity: 1; transform: scale(1.15); }\n.iwb-whitebg-btn svg { width: 14px; height: 14px; display: block; }\n.iwb-whitebg-btn.processing { animation: iwb-cutout-pulse 1s infinite; pointer-events: none; }\n\n/* 编辑按钮 */\n.iwb-editor-btn-icon {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-editor-btn-icon:hover { opacity: 1; transform: scale(1.15); }\n.iwb-editor-btn-icon svg { width: 14px; height: 14px; display: block; }\n\n/* ==================== 图片编辑器 ==================== */\n\n.iwb-editor-overlay {\n position: fixed;\n top: 0; left: 0;\n width: 100%; height: 100%;\n background: rgba(0, 0, 0, 0.6);\n display: none;\n align-items: center;\n justify-content: center;\n z-index: 999998;\n backdrop-filter: blur(3px);\n}\n\n.iwb-editor-modal {\n background: #1e1e2e;\n border-radius: 0;\n box-shadow: none;\n display: flex;\n flex-direction: column;\n width: 100vw;\n height: 100vh;\n max-width: none;\n max-height: none;\n overflow: visible;\n}\n\n/* 新增:主区域(画布 + 图层面板)flex 布局 */\n.iwb-editor-main {\n display: flex;\n flex: 1 1 auto;\n min-height: 0;\n overflow: visible;\n}\n\n.iwb-editor-header {\n display: flex;\n align-items: center;\n gap: 12px;\n padding: 10px 16px;\n background: #181825;\n border-bottom: 1px solid #313244;\n flex-wrap: nowrap;\n}\n.iwb-editor-header > .iwb-editor-tools {\n position: absolute;\n left: 12px;\n top: 68px;\n width: 44px;\n flex-direction: column;\n padding: 6px;\n background: #181825;\n border: 1px solid #313244;\n border-radius: 8px;\n z-index: 20;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action { position: static; }\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="redo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="reset"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="restore"] {\n position: absolute;\n top: -54px;\n left: auto;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"] { right: 154px; }\n.iwb-editor-header > .iwb-edLine truncated
var editor_default = '/* IWB 鲜艺抠图节点 - 注入样式 */\n\n/* 抠图按钮 */\n.iwb-cutout-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n\n.iwb-cutout-btn:hover {\n opacity: 1;\n transform: scale(1.15);\n}\n\n.iwb-cutout-btn svg {\n width: 14px;\n height: 14px;\n display: block;\n}\n\n/* 正在处理中的按钮状态 */\n.iwb-cutout-btn.processing {\n animation: iwb-cutout-pulse 1s infinite;\n pointer-events: none;\n}\n\n@keyframes iwb-cutout-pulse {\n 0%, 100% { opacity: 0.4; }\n 50% { opacity: 1; }\n}\n\n/* 全屏遮罩 */\n.iwb-cutout-overlay {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.25);\n display: none;\n align-items: center;\n justify-content: center;\n flex-direction: column;\n gap: 12px;\n z-index: 999999;\n pointer-events: all;\n backdrop-filter: blur(2px);\n}\n\n/* 自定义按钮提示(挂在 body,最高层级,不会被图片遮挡) */\n#iwb-cutout-tip {\n position: fixed;\n z-index: 2147483647;\n background: rgba(30, 30, 46, 0.95);\n color: #fff;\n padding: 5px 10px;\n border-radius: 6px;\n font-size: 12px;\n line-height: 1.5;\n white-space: nowrap;\n pointer-events: none;\n display: none;\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);\n border: 1px solid rgba(255, 255, 255, 0.14);\n font-family: system-ui, -apple-system, "Segoe UI", sans-serif;\n max-width: 320px;\n white-space: normal;\n word-break: break-all;\n}\n\n/* 加载动画 */\n.iwb-cutout-spinner {\n width: 48px;\n height: 48px;\n border: 4px solid rgba(255, 255, 255, 0.3);\n border-top-color: #e8a735;\n border-radius: 50%;\n animation: iwb-cutout-spin 0.8s linear infinite;\n}\n\n@keyframes iwb-cutout-spin {\n to { transform: rotate(360deg); }\n}\n\n/* 遮罩文字 */\n.iwb-cutout-text {\n color: #fff;\n font-size: 14px;\n font-weight: 500;\n text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);\n background: rgba(0, 0, 0, 0.6);\n padding: 8px 16px;\n border-radius: 6px;\n}\n\n/* Toast 提示 */\n.iwb-cutout-toast {\n position: fixed;\n bottom: 24px;\n left: 50%;\n transform: translateX(-50%);\n padding: 10px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-weight: 500;\n z-index: 999999;\n transition: opacity 0.3s;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);\n display: none;\n max-width: 400px;\n}\n\n.iwb-cutout-toast-success {\n background: #1a7f37;\n color: #fff;\n}\n\n.iwb-cutout-toast-error {\n background: #cf222e;\n color: #fff;\n}\n\n.iwb-cutout-toast-info {\n background: #1f6feb;\n color: #fff;\n}\n\n/* 白底按钮 */\n.iwb-whitebg-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-whitebg-btn:hover { opacity: 1; transform: scale(1.15); }\n.iwb-whitebg-btn svg { width: 14px; height: 14px; display: block; }\n.iwb-whitebg-btn.processing { animation: iwb-cutout-pulse 1s infinite; pointer-events: none; }\n\n/* 编辑按钮 */\n.iwb-editor-btn-icon {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-editor-btn-icon:hover { opacity: 1; transform: scale(1.15); }\n.iwb-editor-btn-icon svg { width: 14px; height: 14px; display: block; }\n\n/* ==================== 图片编辑器 ==================== */\n\n.iwb-editor-overlay {\n position: fixed;\n top: 0; left: 0;\n width: 100%; height: 100%;\n background: rgba(0, 0, 0, 0.6);\n display: none;\n align-items: center;\n justify-content: center;\n z-index: 999998;\n backdrop-filter: blur(3px);\n}\n\n.iwb-editor-modal {\n background: #1e1e2e;\n border-radius: 0;\n box-shadow: none;\n display: flex;\n flex-direction: column;\n width: 100vw;\n height: 100vh;\n max-width: none;\n max-height: none;\n overflow: visible;\n}\n\n/* 新增:主区域(画布 + 图层面板)flex 布局 */\n.iwb-editor-main {\n display: flex;\n flex: 1 1 auto;\n min-height: 0;\n overflow: visible;\n}\n\n.iwb-editor-header {\n display: flex;\n align-items: center;\n gap: 12px;\n padding: 10px 16px;\n background: #181825;\n border-bottom: 1px solid #313244;\n flex-wrap: nowrap;\n}\n.iwb-editor-header > .iwb-editor-tools {\n position: absolute;\n left: 12px;\n top: 68px;\n width: 44px;\n flex-direction: column;\n padding: 6px;\n background: #181825;\n border: 1px solid #313244;\n border-radius: 8px;\n z-index: 20;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action { position: static; }\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="redo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="reset"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="restore"] {\n position: absolute;\n top: -54px;\n left: auto;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"] { right: 154px; }\n.iwb-editor-header > .iwb-edLine truncated
// image-annotate/plugin/glue.js
function createGlue(ctx, css) {