-
Notifications
You must be signed in to change notification settings - Fork 104
chore(image-toolbox): release v2.3 #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MoruTeaven
wants to merge
3
commits into
ZToolsCenter:main
Choose a base branch
from
MoruTeaven:feat/image-toolbox-v2.3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import eventBus from './EventBus.js'; | ||
| import eventBus from './EventBus.js'; | ||
|
|
||
| /** | ||
| * 图层管理器 — 管理 Fabric.js 物件的 z-order、显隐、锁定 | ||
|
|
@@ -7,6 +7,7 @@ | |
| class LayerManager { | ||
| constructor(canvasManager) { | ||
| this._cm = canvasManager; | ||
| this._cm.layerManager = this; | ||
| this._layers = []; // 图层元数据 [{ id, name, visible, locked, fabricObj }] | ||
| this._idCounter = 0; | ||
| } | ||
|
|
@@ -43,19 +44,22 @@ class LayerManager { | |
| meta.fabricObj = obj; | ||
| this._refreshMetaName(meta, obj, false, newLayers, currentObjects); | ||
| } | ||
| this._refreshMetaState(meta, obj, false); | ||
| meta.zIndex = objects.length - 1 - i; | ||
| newLayers.push(meta); | ||
| } | ||
|
|
||
| // 背景图层始终在列表末尾(面板最底部) | ||
| if (this._cm.originalImage) { | ||
| this._ensureBackgroundSelectable(this._cm.originalImage); | ||
| let bgMeta = oldLayers.find(l => l.fabricObj === this._cm.originalImage) || null; | ||
| if (!bgMeta) { | ||
| bgMeta = this._createMeta(this._cm.originalImage, true, newLayers, currentObjects); | ||
| } else { | ||
| bgMeta.fabricObj = this._cm.originalImage; | ||
| this._refreshMetaName(bgMeta, this._cm.originalImage, true, newLayers, currentObjects); | ||
| } | ||
| this._refreshMetaState(bgMeta, this._cm.originalImage, true); | ||
| bgMeta.zIndex = 0; | ||
| newLayers.push(bgMeta); | ||
| } | ||
|
|
@@ -106,20 +110,55 @@ class LayerManager { | |
| const meta = { | ||
| id, | ||
| name: nameInfo.name, | ||
| visible: obj.visible !== false, | ||
| locked: isBackground ? true : (!obj.selectable && !obj.evented), | ||
| visible: true, | ||
| locked: false, | ||
| fabricObj: obj, | ||
| zIndex: 0, | ||
| isBackground, | ||
| }; | ||
|
|
||
| this._refreshMetaState(meta, obj, isBackground); | ||
|
|
||
| if (!isBackground) { | ||
| this._setObjectLayerName(obj, meta.name, nameInfo.auto, nameInfo.baseName); | ||
| } | ||
|
|
||
| return meta; | ||
| } | ||
|
|
||
| _refreshMetaState(meta, obj, isBackground = false) { | ||
| meta.visible = obj.visible !== false; | ||
| meta.locked = this._resolveLockedState(obj, isBackground); | ||
| meta.isBackground = !!isBackground; | ||
| } | ||
|
Comment on lines
+129
to
+133
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在 _refreshMetaState(meta, obj, isBackground = false) {
meta.visible = obj.visible !== false;
meta.locked = this._resolveLockedState(obj, isBackground);
meta.isBackground = !!isBackground;
}
getLayerByObject(obj) {
return this._layers.find(l => l.fabricObj === obj) || null;
} |
||
|
|
||
| _resolveLockedState(obj, isBackground = false) { | ||
| if (isBackground) return true; | ||
| if (typeof obj?._layerLocked === 'boolean') return obj._layerLocked; | ||
|
|
||
| // 旧版本把工具激活期间创建的图层也标成 selectable/evented=false。 | ||
| // 没有明确锁定标记时按未锁定处理,避免移动/框选工具无法选中这些图层。 | ||
| return false; | ||
| } | ||
|
|
||
| _ensureBackgroundSelectable(obj) { | ||
| if (!obj) return; | ||
|
|
||
| obj._originalImage = true; | ||
| obj.set({ | ||
| selectable: true, | ||
| evented: true, | ||
| hasControls: true, | ||
| hasBorders: true, | ||
| lockMovementX: false, | ||
| lockMovementY: false, | ||
| lockRotation: false, | ||
| lockScalingX: false, | ||
| lockScalingY: false, | ||
| }); | ||
| obj.setCoords(); | ||
| } | ||
|
|
||
| _refreshMetaName(meta, obj, isBackground = false, newLayers = null, currentObjects = null) { | ||
| const nameInfo = this._resolveLayerName(obj, isBackground, newLayers, meta, currentObjects); | ||
| meta.name = nameInfo.name; | ||
|
|
@@ -378,6 +417,7 @@ class LayerManager { | |
| if (!meta || meta.isBackground) return; | ||
|
|
||
| meta.locked = !!locked; | ||
| meta.fabricObj._layerLocked = meta.locked; | ||
| meta.fabricObj.set({ | ||
| selectable: !meta.locked, | ||
| evented: !meta.locked, | ||
|
|
@@ -398,9 +438,13 @@ class LayerManager { | |
| const meta = this._layers.find(l => l.id === layerId); | ||
| if (!meta) return; | ||
|
|
||
| // 即使图层被锁定也触发事件(让橡皮擦等工具能响应图层切换), | ||
| // 但不调用 setActiveObject(避免误操作锁定图层)。 | ||
| if (!meta.locked) { | ||
| // 即使图层被锁定也触发事件(让橡皮擦等工具能响应图层切换)。 | ||
| // 普通锁定图层不调用 setActiveObject(避免误操作); | ||
| // 但背景图层允许选中和变换;图层锁定只限制删除、改名和排序。 | ||
| if (!meta.locked || meta.isBackground) { | ||
| if (meta.isBackground) { | ||
| this._ensureBackgroundSelectable(meta.fabricObj); | ||
| } | ||
| this._cm.canvas.setActiveObject(meta.fabricObj); | ||
| this._cm.canvas.renderAll(); | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在
_isSameSnapshot中,代码使用JSON.stringify(a) === JSON.stringify(b)来对比两个画布快照是否相同。\n\n对于包含大图(如 base64 格式的背景图或高分辨率贴图)的画布,JSON.stringify序列化会非常消耗 CPU 和内存,在频繁保存历史状态(如自由绘制、拖拽等)时会导致明显的界面卡顿。\n\n建议未来考虑引入轻量级的版本号/修改计数器(Revision Counter)机制,或者在对比时过滤掉图片数据源(src)等大体积的静态属性,以避免高频的深度序列化对比。