@blocksuite/phasor
Advanced tools
Comparing version 0.0.0-20230824020044-afa5db2e-nightly to 0.0.0-20230824081236-b25a3da9-nightly
@@ -82,4 +82,6 @@ import { DEFAULT_ROUGHNESS, StrokeStyle } from '../../consts.js'; | ||
// eg. undo/redo, copy/paste | ||
if (!points.length) | ||
if (!points.length || points.length < 2) { | ||
console.warn('connector points not ready yet, there is something wrong.'); | ||
return; | ||
} | ||
const last = points[points.length - 1]; | ||
@@ -86,0 +88,0 @@ const secondToLast = points[points.length - 2]; |
@@ -43,2 +43,3 @@ import { Slot } from '@blocksuite/global/utils'; | ||
private _onYContainer; | ||
private _onYEvent; | ||
private _transact; | ||
@@ -45,0 +46,0 @@ refresh(): void; |
@@ -26,34 +26,46 @@ import { assertExists, Slot } from '@blocksuite/global/utils'; | ||
return; | ||
const connectors = []; | ||
event.keysChanged.forEach(id => { | ||
const type = event.changes.keys.get(id); | ||
if (!type) { | ||
const change = event.changes.keys.get(id); | ||
if (!change) { | ||
console.error('invalid event', event); | ||
return; | ||
} | ||
if (type.action === 'add') { | ||
const yElement = this._yContainer.get(id); | ||
const type = yElement.get('type'); | ||
const ElementCtor = ElementCtors[type]; | ||
assertExists(ElementCtor); | ||
const element = new ElementCtor(yElement, this); | ||
element.computedValue = this._computedValue; | ||
element.mount(this._renderer); | ||
this._elements.set(element.id, element); | ||
this._addToBatch(element); | ||
this.slots.elementAdded.emit(id); | ||
if (change.action === 'add' && | ||
this._yContainer.get(id)?.get('type') === 'connector') { | ||
connectors.push({ change, id }); | ||
return; | ||
} | ||
else if (type.action === 'update') { | ||
console.error('update event on yElements is not supported', event); | ||
else { | ||
this._onYEvent(change, id); | ||
} | ||
else if (type.action === 'delete') { | ||
const element = this._elements.get(id); | ||
assertExists(element); | ||
element.unmount(); | ||
this._elements.delete(id); | ||
this.deleteElementLocalRecord(id); | ||
this._removeFromBatch(element); | ||
this.slots.elementRemoved.emit({ id, element }); | ||
} | ||
}); | ||
connectors.forEach(({ change, id }) => this._onYEvent(change, id)); | ||
}; | ||
this._onYEvent = (type, id) => { | ||
if (type.action === 'add') { | ||
const yElement = this._yContainer.get(id); | ||
const type = yElement.get('type'); | ||
const ElementCtor = ElementCtors[type]; | ||
assertExists(ElementCtor); | ||
const element = new ElementCtor(yElement, this); | ||
element.computedValue = this._computedValue; | ||
element.mount(this._renderer); | ||
this._elements.set(element.id, element); | ||
this._addToBatch(element); | ||
this.slots.elementAdded.emit(id); | ||
} | ||
else if (type.action === 'update') { | ||
console.error('update event on yElements is not supported', event); | ||
} | ||
else if (type.action === 'delete') { | ||
const element = this._elements.get(id); | ||
assertExists(element); | ||
element.unmount(); | ||
this._elements.delete(id); | ||
this.deleteElementLocalRecord(id); | ||
this._removeFromBatch(element); | ||
this.slots.elementRemoved.emit({ id, element }); | ||
} | ||
}; | ||
this._renderer = new Renderer(); | ||
@@ -60,0 +72,0 @@ this._yContainer = yContainer; |
{ | ||
"name": "@blocksuite/phasor", | ||
"version": "0.0.0-20230824020044-afa5db2e-nightly", | ||
"version": "0.0.0-20230824081236-b25a3da9-nightly", | ||
"description": "Hybrid canvas renderer.", | ||
@@ -28,3 +28,3 @@ "main": "dist/index.js", | ||
"fractional-indexing": "^3.2.0", | ||
"@blocksuite/global": "0.0.0-20230824020044-afa5db2e-nightly" | ||
"@blocksuite/global": "0.0.0-20230824081236-b25a3da9-nightly" | ||
}, | ||
@@ -31,0 +31,0 @@ "scripts": { |
@@ -125,3 +125,6 @@ import { DEFAULT_ROUGHNESS, StrokeStyle } from '../../consts.js'; | ||
// eg. undo/redo, copy/paste | ||
if (!points.length) return; | ||
if (!points.length || points.length < 2) { | ||
console.warn('connector points not ready yet, there is something wrong.'); | ||
return; | ||
} | ||
@@ -128,0 +131,0 @@ const last = points[points.length - 1]; |
@@ -129,5 +129,11 @@ import { assertExists, Slot } from '@blocksuite/global/utils'; | ||
if (event.changes.keys.size === 0) return; | ||
const connectors: { | ||
change: (typeof event)['changes']['keys'] extends Map<string, infer V> | ||
? V | ||
: never; | ||
id: string; | ||
}[] = []; | ||
event.keysChanged.forEach(id => { | ||
const type = event.changes.keys.get(id); | ||
if (!type) { | ||
const change = event.changes.keys.get(id); | ||
if (!change) { | ||
console.error('invalid event', event); | ||
@@ -137,28 +143,49 @@ return; | ||
if (type.action === 'add') { | ||
const yElement = this._yContainer.get(id) as Y.Map<unknown>; | ||
const type = yElement.get('type') as keyof PhasorElementType; | ||
if ( | ||
change.action === 'add' && | ||
this._yContainer.get(id)?.get('type') === 'connector' | ||
) { | ||
connectors.push({ change, id }); | ||
return; | ||
} else { | ||
this._onYEvent(change, id); | ||
} | ||
}); | ||
connectors.forEach(({ change, id }) => this._onYEvent(change, id)); | ||
}; | ||
const ElementCtor = ElementCtors[type]; | ||
assertExists(ElementCtor); | ||
const element = new ElementCtor(yElement, this); | ||
element.computedValue = this._computedValue; | ||
element.mount(this._renderer); | ||
this._elements.set(element.id, element); | ||
private _onYEvent = ( | ||
type: Y.YMapEvent<Y.Map<unknown>>['changes']['keys'] extends Map< | ||
string, | ||
infer V | ||
> | ||
? V | ||
: never, | ||
id: string | ||
) => { | ||
if (type.action === 'add') { | ||
const yElement = this._yContainer.get(id) as Y.Map<unknown>; | ||
const type = yElement.get('type') as keyof PhasorElementType; | ||
this._addToBatch(element); | ||
this.slots.elementAdded.emit(id); | ||
} else if (type.action === 'update') { | ||
console.error('update event on yElements is not supported', event); | ||
} else if (type.action === 'delete') { | ||
const element = this._elements.get(id); | ||
assertExists(element); | ||
const ElementCtor = ElementCtors[type]; | ||
assertExists(ElementCtor); | ||
const element = new ElementCtor(yElement, this); | ||
element.computedValue = this._computedValue; | ||
element.mount(this._renderer); | ||
this._elements.set(element.id, element); | ||
element.unmount(); | ||
this._elements.delete(id); | ||
this.deleteElementLocalRecord(id); | ||
this._removeFromBatch(element); | ||
this.slots.elementRemoved.emit({ id, element }); | ||
} | ||
}); | ||
this._addToBatch(element); | ||
this.slots.elementAdded.emit(id); | ||
} else if (type.action === 'update') { | ||
console.error('update event on yElements is not supported', event); | ||
} else if (type.action === 'delete') { | ||
const element = this._elements.get(id); | ||
assertExists(element); | ||
element.unmount(); | ||
this._elements.delete(id); | ||
this.deleteElementLocalRecord(id); | ||
this._removeFromBatch(element); | ||
this.slots.elementRemoved.emit({ id, element }); | ||
} | ||
}; | ||
@@ -165,0 +192,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1022573
18064
+ Added@blocksuite/global@0.0.0-20230824081236-b25a3da9-nightly(transitive)
- Removed@blocksuite/global@0.0.0-20230824020044-afa5db2e-nightly(transitive)
Updated@blocksuite/global@0.0.0-20230824081236-b25a3da9-nightly