|
| 1 | +function fitsConstraint(constraints, type, width) { |
| 2 | + const constraint = constraints[type]; |
| 3 | + if(constraint) { |
| 4 | + return width >= (constraint.min ?? 0) && width <= (constraint.max ?? Infinity) |
| 5 | + } |
| 6 | + return true; |
| 7 | +} |
| 8 | + |
1 | 9 | panel.plugin("rasteiner/conditionalblocks", { |
2 | 10 | use: [ |
3 | 11 | function (Vue) { |
@@ -33,29 +41,31 @@ panel.plugin("rasteiner/conditionalblocks", { |
33 | 41 | }; |
34 | 42 | }; |
35 | 43 |
|
36 | | - const draggableOptions = Blocks.computed.draggableOptions; |
37 | | - delete Blocks.computed.draggableOptions; |
38 | | - |
39 | 44 | Vue.component("k-blocks", { |
40 | 45 | extends: Blocks, |
41 | 46 | inject: ["constraints", "cwidth"], |
| 47 | + methods: { |
| 48 | + append(what, index) { |
| 49 | + if(this.constraints && this.cwidth && Array.isArray(what)) { |
| 50 | + what = what.filter((block) => fitsConstraint(this.constraints, block.type, this.cwidth)); |
| 51 | + } |
| 52 | + |
| 53 | + Blocks.methods.append.call(this, what, index); |
| 54 | + } |
| 55 | + }, |
42 | 56 | computed: { |
43 | 57 | draggableOptions() { |
44 | | - const original = draggableOptions.call(this); |
| 58 | + const original = Blocks.computed.draggableOptions.call(this); |
45 | 59 |
|
46 | 60 | if (this.constraints && this.cwidth) { |
47 | 61 | //remove fieldsets that are not allowed by constraints |
48 | 62 | original.data.fieldsets = Object.fromEntries( |
49 | | - Object.entries(original.data.fieldsets).filter(([type]) => { |
50 | | - const constraint = this.constraints[type]; |
51 | | - return !( |
52 | | - (constraint?.min && this.cwidth < constraint.min) || |
53 | | - (constraint?.max && this.cwidth > constraint.max) |
54 | | - ); |
55 | | - }) |
| 63 | + Object.entries(original.data.fieldsets).filter(([type]) => fitsConstraint(this.constraints, type, this.cwidth)) |
56 | 64 | ); |
| 65 | + console.log(original.data.fieldsets); |
57 | 66 | } |
58 | 67 |
|
| 68 | + |
59 | 69 | return original; |
60 | 70 | }, |
61 | 71 | }, |
|
0 commit comments