Skip to content

Commit

Permalink
Fix #117 remove entity
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinsw committed Jan 25, 2024
1 parent 61cb37f commit 9b63410
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
16 changes: 7 additions & 9 deletions src/lib/components/CrateEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,20 @@ function updateEntity(entity, prop, value) {
}
}
function deleteEntity() {
async function deleteEntity() {
//delete
//count the links
const linksCount = countReverse(data.entity);
const entityMessage = linksCount > 1 ? 'entities' : 'entity';
if (linksCount === 0 || window.confirm(`This entity is referenced by ${linksCount} other ${entityMessage}. Are you sure you want to delete it?`)) {
// const i = state.entities.findIndex(e => e['@id'] === data.entity['@id']);
// if (i >= 0) state.entities.splice(i, 1);
state.entities.value.delete(data.entity['@id']);
nextTick(() => {
data.history.pop();
state.crate.deleteEntity(data.entity, { references: true });
const prevEntity = data.history[data.history.length - 1] ?? data.rootDataset;
emit("update:crate", props.crate);
emit("update:entityId", prevEntity['@id']);
})
const e = data.entity;
data.history.pop();
data.entity = data.history[data.history.length - 1] ?? data.rootDataset;
await state.deleteEntity(e);
emit("update:entityId", data.entity);
emit("update:crate", props.crate);
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/lib/components/EditorState.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ref, reactive, shallowReactive, isReactive } from 'vue';
import { ref, reactive, shallowReactive, isReactive, nextTick } from 'vue';
import { ROCrate } from 'ro-crate';
import { ElCheckbox } from 'element-plus';
import InputDateTime from '../components/InputDateTime.vue';
Expand Down Expand Up @@ -132,6 +132,12 @@ export class EditorState {
this.entities.value = new Set(this.crate.entities({ filter: e => e['@id'] !== mid }));
}

async deleteEntity(e) {
this.entities.value.delete(e);
await nextTick();
this.crate.deleteEntity(e, { references: true });
}

/**
* Get property definitions based on type as defined in profile
* @param {string[]} types One or more (combined) types associated with an entity
Expand Down Expand Up @@ -338,4 +344,3 @@ export class EditorState {
}
}
}

8 changes: 3 additions & 5 deletions src/lib/components/Property.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,12 @@ function removeValue(i, value) {
vals.splice(i, 1);
props.components.splice(i, 1);
if (typeof value === 'object' && value['@id']) {
if (!countReverse(toRaw(value))) {
let count = countReverse(toRaw(value));
if (count <= 1) {
//todo: confirm to delete entity
//const i = state.entities.findIndex(e => e['@id'] === value['@id']);
//if (i >= 0) state.entities.splice(i, 1);
state.entities.value.delete(value)
nextTick(() => {
state.crate.deleteEntity(value);
});
state.deleteEntity(value);
}
}
emit('update:modelValue', vals);
Expand Down

0 comments on commit 9b63410

Please sign in to comment.