Skip to content
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

Use structuredClone #6086

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion panel/lab/components/stats/1_stats/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default {
];
},
reportsWithoutTheme() {
return this.$helper.clone(this.reports).map((report) => {
return structuredClone(this.reports).map((report) => {
report.icon = null;
report.theme = null;
return report;
Expand Down
6 changes: 3 additions & 3 deletions panel/src/components/Forms/Blocks/Blocks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export default {
},
async duplicate(block, index) {
const copy = {
...this.$helper.clone(block),
...structuredClone(block),
id: this.$helper.uuid()
};
this.blocks.splice(index + 1, 0, copy);
Expand Down Expand Up @@ -669,7 +669,7 @@ export default {
if (to < 0) {
return;
}
let blocks = this.$helper.clone(this.blocks);
let blocks = structuredClone(this.blocks);
blocks.splice(from, 1);
blocks.splice(to, 0, block);
this.blocks = blocks;
Expand All @@ -679,7 +679,7 @@ export default {
},
async split(block, index, contents) {
// prepare old block with reduced content chunk
const oldBlock = this.$helper.clone(block);
const oldBlock = structuredClone(block);
oldBlock.content = { ...oldBlock.content, ...contents[0] };

// create a new block and merge in default contents as
Expand Down
4 changes: 2 additions & 2 deletions panel/src/components/Forms/Input/TagsInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default {
// no new tags if this is full,
// check if the tag is accepted
if (this.isAllowed(tag) === true) {
const tags = this.$helper.object.clone(this.value);
const tags = structuredClone(this.value);
tags.push(tag.value);
this.$emit("input", tags);
}
Expand Down Expand Up @@ -182,7 +182,7 @@ export default {
}

// replace the tag at the given index
const tags = this.$helper.object.clone(this.value);
const tags = structuredClone(this.value);
tags.splice(index, 1, updated.value);
this.$emit("input", tags);

Expand Down
4 changes: 2 additions & 2 deletions panel/src/components/Forms/Layouts/Layouts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default {
});
},
duplicate(index, layout) {
const copy = this.$helper.clone(layout);
const copy = structuredClone(layout);

// replace all unique IDs for layouts, columns and blocks
// the method processes a single object and returns it as an array
Expand Down Expand Up @@ -192,7 +192,7 @@ export default {
// move throught the new layout rows in steps of columns per row
for (let i = 0; i < chunks; i += newLayout.columns.length) {
const copy = {
...this.$helper.clone(newLayout),
...structuredClone(newLayout),
id: this.$helper.uuid()
};

Expand Down
2 changes: 1 addition & 1 deletion panel/src/components/Navigation/Tags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default {
handler() {
// make sure values are not reactive
// otherwise this could have nasty side-effects
let tags = this.$helper.object.clone(this.value);
let tags = structuredClone(this.value);

// sort all tags by the available options
if (this.sort === true) {
Expand Down
4 changes: 1 addition & 3 deletions panel/src/components/Views/System/SystemSecurity.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
</template>

<script>
import { clone } from "@/helpers/object.js";

/**
* @internal
* @since 4.0.0
Expand All @@ -42,7 +40,7 @@ export default {
},
data() {
return {
issues: clone(this.security)
issues: structuredClone(this.security)
};
},
async created() {
Expand Down
4 changes: 1 addition & 3 deletions panel/src/helpers/field.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { clone } from "./object.js";

/**
* Loads the default value for a field definition
* @param {Object} field
* @returns {mixed}
*/
export function defaultValue(field) {
if (field.default !== undefined) {
return clone(field.default);
return structuredClone(field.default);
}

const component =
Expand Down
10 changes: 6 additions & 4 deletions panel/src/helpers/object.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/**
* Clone provided object or array
*
* @param {Object|array} array
* @param {Object|array} value
* @returns {Object|array}
*
* @deprecated Use `structuredClone` instead
*/
export function clone(array) {
if (array === undefined) {
export function clone(value) {
if (value === undefined) {
return undefined;
}

return JSON.parse(JSON.stringify(array));
return structuredClone(value);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions panel/src/store/modules/content.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { set, del } from "vue";
import { clone, length } from "@/helpers/object.js";
import { length } from "@/helpers/object.js";

const keep = (id, data) => {
localStorage.setItem("kirby$content$" + id, JSON.stringify(data));
Expand Down Expand Up @@ -86,7 +86,7 @@ export default {
* Returns original (in content file) values for passed model ID
*/
originals: (state, getters) => (id) => {
return clone(getters.model(id).originals);
return structuredClone(getters.model(id).originals);
},
/**
* Returns values (incl. unsaved changes) for passed model ID
Expand All @@ -101,7 +101,7 @@ export default {
* Returns unsaved changes for passed model ID
*/
changes: (state, getters) => (id) => {
return clone(getters.model(id).changes);
return structuredClone(getters.model(id).changes);
}
},

Expand Down Expand Up @@ -138,7 +138,7 @@ export default {
},
MOVE(state, [from, to]) {
// move state
const model = clone(state.models[from]);
const model = structuredClone(state.models[from]);
del(state.models, from);
set(state.models, to, model);

Expand Down Expand Up @@ -171,7 +171,7 @@ export default {
value = null;
}

value = clone(value);
value = structuredClone(value);

// // compare current field value with its original value
const current = JSON.stringify(value);
Expand Down Expand Up @@ -245,7 +245,7 @@ export default {
context.commit("CLEAR");
},
create(context, model) {
const content = clone(model.content);
const content = structuredClone(model.content);

// remove fields from the content object that
// should be ignored in changes or when saving content
Expand Down