mirror of
https://github.com/umap-project/umap.git
synced 2025-04-28 19:42:36 +02:00
wip
Co-authored-by: Alexis Métaireau <alexis@notmyidea.org>
This commit is contained in:
parent
3ad676ed59
commit
a09984e340
2 changed files with 65 additions and 16 deletions
|
@ -22,12 +22,41 @@ export class UndoManager {
|
||||||
this.toggleState()
|
this.toggleState()
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanOperation(operation, redo) {
|
markSaved() {
|
||||||
const syncOperation = Utils.CopyJSON(operation)
|
if (this._undoStack.length > 0) {
|
||||||
delete syncOperation.oldValue
|
const lastOperation = this._undoStack[this._undoStack.length - 1]
|
||||||
delete syncOperation.newValue
|
lastOperation.saved_marker = true
|
||||||
syncOperation.value = redo ? operation.newValue : operation.oldValue
|
}
|
||||||
return syncOperation
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the list of changed "subjects" from the undo stack,
|
||||||
|
* since the last time we marked "saved"
|
||||||
|
**/
|
||||||
|
getChangedObjects() {
|
||||||
|
// Get operations in the undo stack since the last save
|
||||||
|
let last_save_index = this._undoStack.findLastIndex(
|
||||||
|
(op) => op.saved_marker === true
|
||||||
|
)
|
||||||
|
if (last_save_index === -1) {
|
||||||
|
last_save_index = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('last save index', last_save_index)
|
||||||
|
const operations_since_last_saved = this._undoStack.slice(last_save_index)
|
||||||
|
|
||||||
|
return operations_since_last_saved.reduce((acc, op) => {
|
||||||
|
const metadata = { subject: op.subject, metadata: op.metadata }
|
||||||
|
const obj = this._getSaveTargetFromOperation(op)
|
||||||
|
if (!acc.includes(obj)) {
|
||||||
|
acc.push(obj)
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, [])
|
||||||
|
}
|
||||||
|
|
||||||
|
_getSaveTargetFromOperation({ subject, metadata }) {
|
||||||
|
return this._getUpdater(subject, metadata).getSaveTarget(metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
undo(redo = false) {
|
undo(redo = false) {
|
||||||
|
@ -37,10 +66,10 @@ export class UndoManager {
|
||||||
if (!operation) return
|
if (!operation) return
|
||||||
if (operation.verb === 'batch') {
|
if (operation.verb === 'batch') {
|
||||||
for (const op of operation.operations) {
|
for (const op of operation.operations) {
|
||||||
this.applyOperation(this.cleanOperation(op, redo))
|
this._applyOperation(this._cleanOperation(op, redo))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.applyOperation(this.cleanOperation(operation, redo))
|
this._applyOperation(this._cleanOperation(operation, redo))
|
||||||
}
|
}
|
||||||
toStack.push(operation)
|
toStack.push(operation)
|
||||||
this.toggleState()
|
this.toggleState()
|
||||||
|
@ -50,7 +79,15 @@ export class UndoManager {
|
||||||
this.undo(true)
|
this.undo(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
applyOperation(syncOperation) {
|
_cleanOperation(operation, redo) {
|
||||||
|
const syncOperation = Utils.CopyJSON(operation)
|
||||||
|
delete syncOperation.oldValue
|
||||||
|
delete syncOperation.newValue
|
||||||
|
syncOperation.value = redo ? operation.newValue : operation.oldValue
|
||||||
|
return syncOperation
|
||||||
|
}
|
||||||
|
|
||||||
|
_applyOperation(syncOperation) {
|
||||||
const updater = this._getUpdater(syncOperation.subject, syncOperation.metadata)
|
const updater = this._getUpdater(syncOperation.subject, syncOperation.metadata)
|
||||||
switch (syncOperation.verb) {
|
switch (syncOperation.verb) {
|
||||||
case 'update':
|
case 'update':
|
||||||
|
|
|
@ -31,8 +31,8 @@ class BaseUpdater {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getDataLayerFromID(layerId) {
|
getDataLayerFromMetadata({ id }) {
|
||||||
return this._umap.getDataLayerByUmapId(layerId)
|
return this._umap.getDataLayerByUmapId(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
applyMessage(payload) {
|
applyMessage(payload) {
|
||||||
|
@ -51,6 +51,10 @@ export class MapUpdater extends BaseUpdater {
|
||||||
this._umap.onPropertiesUpdated([key])
|
this._umap.onPropertiesUpdated([key])
|
||||||
this._umap.render([key])
|
this._umap.render([key])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSaveTarget(metadata) {
|
||||||
|
return this._umap
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DataLayerUpdater extends BaseUpdater {
|
export class DataLayerUpdater extends BaseUpdater {
|
||||||
|
@ -60,7 +64,7 @@ export class DataLayerUpdater extends BaseUpdater {
|
||||||
console.log(
|
console.log(
|
||||||
'found datalayer with id',
|
'found datalayer with id',
|
||||||
value.id,
|
value.id,
|
||||||
this.getDataLayerFromID(value.id)
|
this.getDataLayerFromMetadata(value)
|
||||||
)
|
)
|
||||||
} catch {
|
} catch {
|
||||||
console.log('we are the fucking catch', value)
|
console.log('we are the fucking catch', value)
|
||||||
|
@ -72,7 +76,7 @@ export class DataLayerUpdater extends BaseUpdater {
|
||||||
}
|
}
|
||||||
|
|
||||||
update({ key, metadata, value }) {
|
update({ key, metadata, value }) {
|
||||||
const datalayer = this.getDataLayerFromID(metadata.id)
|
const datalayer = this.getDataLayerFromMetadata(metadata)
|
||||||
if (fieldInSchema(key)) {
|
if (fieldInSchema(key)) {
|
||||||
this.updateObjectValue(datalayer, key, value)
|
this.updateObjectValue(datalayer, key, value)
|
||||||
} else {
|
} else {
|
||||||
|
@ -85,17 +89,21 @@ export class DataLayerUpdater extends BaseUpdater {
|
||||||
}
|
}
|
||||||
|
|
||||||
delete({ metadata }) {
|
delete({ metadata }) {
|
||||||
const datalayer = this.getDataLayerFromID(metadata.id)
|
const datalayer = this.getDataLayerFromMetadata(metadata)
|
||||||
if (datalayer) {
|
if (datalayer) {
|
||||||
datalayer.del(false)
|
datalayer.del(false)
|
||||||
datalayer.commitDelete()
|
datalayer.commitDelete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSaveTarget(metadata) {
|
||||||
|
return this.getDataLayerFromMetadata(metadata)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FeatureUpdater extends BaseUpdater {
|
export class FeatureUpdater extends BaseUpdater {
|
||||||
getFeatureFromMetadata({ id, layerId }) {
|
getFeatureFromMetadata({ id, layerId }) {
|
||||||
const datalayer = this.getDataLayerFromID(layerId)
|
const datalayer = this.getDataLayerFromMetadata({ id: layerId })
|
||||||
return datalayer.getFeatureById(id)
|
return datalayer.getFeatureById(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,7 +111,7 @@ export class FeatureUpdater extends BaseUpdater {
|
||||||
upsert({ metadata, value }) {
|
upsert({ metadata, value }) {
|
||||||
console.log('updater.upsert for', metadata, value)
|
console.log('updater.upsert for', metadata, value)
|
||||||
const { id, layerId } = metadata
|
const { id, layerId } = metadata
|
||||||
const datalayer = this.getDataLayerFromID(layerId)
|
const datalayer = this.getDataLayerFromMetadata({ id: layerId })
|
||||||
const feature = this.getFeatureFromMetadata(metadata)
|
const feature = this.getFeatureFromMetadata(metadata)
|
||||||
console.log('feature', feature)
|
console.log('feature', feature)
|
||||||
|
|
||||||
|
@ -139,4 +147,8 @@ export class FeatureUpdater extends BaseUpdater {
|
||||||
const feature = this.getFeatureFromMetadata(metadata)
|
const feature = this.getFeatureFromMetadata(metadata)
|
||||||
if (feature) feature.del(false)
|
if (feature) feature.del(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSaveTarget({ layerId }) {
|
||||||
|
return this.getDataLayerFromMetadata({ id: layerId })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue