Jason Gorst 1 сар өмнө
parent
commit
d9ea86d25d

+ 19 - 0
app/composables/useRevertible.js

@@ -0,0 +1,19 @@
+export function useRevertible(initialValue) {
+  const original = ref()
+  const model = ref()
+  const editedFields = computed(() => findUpdated(original.value, model.value))
+  const isEdited = computed(() => !_isEmpty(editedFields.value))
+  const revert = () => (model.value = original.value)
+
+  // TODO: notify on change
+  watch(
+    initialValue,
+    (newValue) => {
+      original.value = _clone(toValue(newValue))
+      model.value = _clone(toValue(newValue))
+    },
+    { deep: true, immediate: true }
+  )
+
+  return { original, model, editedFields, isEdited, revert }
+}