Skip to content

Commit b81b80f

Browse files
author
Jeffrey Sul
committed
Implemented component duplication feature
1 parent 42507ff commit b81b80f

File tree

9 files changed

+122
-14
lines changed

9 files changed

+122
-14
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ npm-debug.log*
1717
yarn-debug.log*
1818
yarn-error.log*
1919
/coverage
20-
package-lock.json
20+
# package-lock.json
2121
vetur.config.js
2222

2323
# Editor directories and files

src/components/ComponentDisplay.vue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ export default {
137137
}
138138
}
139139
});
140+
// listener for the copy
141+
window.addEventListener('copy', () => {
142+
// if there is an activeComponent, copy info to state using dispatch
143+
if (this.activeComponent) {
144+
console.log('copied!', this.activeComponent);
145+
this.$store.dispatch("copyActiveComponent");
146+
}
147+
});
148+
149+
window.addEventListener('paste', () => {
150+
this.$store.dispatch("pasteActiveComponent");
151+
console.log('pasted');
152+
})
140153
},
141154
142155
computed: {
@@ -391,7 +404,13 @@ export default {
391404
if (event.target.className === "component-display grid-bg") {
392405
if (!(this.activeComponent === "")) this.setActiveComponent("");
393406
}
407+
},
408+
409+
// event handler for copying (ctrl+C)
410+
copyActiveComponent() {
411+
console.log('copied');
394412
}
413+
395414
},
396415
watch: {
397416
activeComponent: function() {

src/components/dashboard_items/Dashboard.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Description:
2020
>
2121
<q-tab name="detail" id="label-text"><i class="fas fa-code"></i></q-tab>
2222
<q-tab name="tree" id="label-text"><i class="fas fa-code-branch fa-flip-vertical" /></q-tab>
23-
<q-tab name="store" id="label-text" ><i class="fas fa-store-alt"></i></q-tab>
23+
<!-- <q-tab name="store" id="label-text" ><i class="fas fa-store-alt"></i></q-tab> -->
2424
</q-tabs>
2525
<q-tab-panels v-model="tab" animated class="html-bg text-white ">
2626
<q-tab-panel name="detail">
@@ -29,9 +29,9 @@ Description:
2929
<q-tab-panel name="tree" >
3030
<Tree />
3131
</q-tab-panel>
32-
<q-tab-panel name="store">
32+
<!-- <q-tab-panel name="store">
3333
<VuexStore />
34-
</q-tab-panel>
34+
</q-tab-panel> -->
3535
</q-tab-panels>
3636
</q-card>
3737
</div>
@@ -41,13 +41,13 @@ Description:
4141
<script>
4242
import { mapState, mapActions } from "vuex";
4343
import Tree from "./Tree";
44-
import VuexStore from "./DashboardVuexStore.vue";
44+
// import VuexStore from "./DashboardVuexStore.vue";
4545
import ComponentDetails from "./ComponentDetails";
4646
4747
export default {
4848
components: {
4949
Tree,
50-
VuexStore,
50+
// VuexStore,
5151
ComponentDetails
5252
},
5353
computed: {

src/components/home_sidebar_items/ComponentTab/ComponentTab.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Functionality includes: if active component is selected, will switch view to editing mode. If not, it will be in create mode -->
33
<template>
44
<q-card id="store-cards">
5-
<!-- <UploadImage v-if="activeComponent === ''" /> -->
5+
<UploadImage v-if="activeComponent === ''" />
66
<CreateComponent v-if="activeComponent === ''"/>
77
<EditDeleteComponents v-if="activeComponent !== ''"/>
88
</q-card>
@@ -12,7 +12,7 @@ Functionality includes: if active component is selected, will switch view to edi
1212
import CreateComponent from './CreateComponent.vue'
1313
import EditDeleteComponents from './EditDeleteComponents.vue'
1414
import { mapState } from 'vuex'
15-
// import UploadImage from '../UploadImage.vue'
15+
import UploadImage from '../UploadImage.vue'
1616
1717
export default {
1818
data () {
@@ -29,7 +29,7 @@ export default {
2929
components: {
3030
CreateComponent,
3131
EditDeleteComponents,
32-
// UploadImage,
32+
UploadImage,
3333
}
3434
}
3535
</script>

src/components/home_sidebar_items/ComponentTab/EditDeleteComponents.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Description:
6464
active-color="secondary"
6565
indicator-color="secondary"
6666
>
67+
<!-- Accordion for component settings -->
6768
<!-- HTML item that has Icon component -->
6869
<q-expansion-item
6970
group="accordion"

src/store/actions.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ const actions = {
7676
commit(types.UPDATE_COMPONENT_SIZE, payload)
7777
},
7878

79+
// copy the active component
80+
[types.copyActiveComponent]: ({ commit }, payload) => {
81+
commit(types.COPY_ACTIVE_COMPONENT, payload)
82+
},
83+
// paste the active component copy
84+
[types.pasteActiveComponent]: ({ commit, state }) => {
85+
commit(types.PASTE_ACTIVE_COMPONENT);
86+
// if no other parents, update as parent of active route in componentMap
87+
if (!Object.keys(state.pastedComponent.parent).length) {
88+
commit(types.ADD_COMPONENT_TO_ACTIVE_ROUTE_CHILDREN, state.pastedComponent.componentName)
89+
// if parents, update parent of pastedComponent to include as a child
90+
} else {
91+
commit(types.ADD_COPIED_PARENT, state.pastedComponent)
92+
};
93+
// add pastedComponent as a child of route in activeRoutes
94+
commit(types.ADD_COMPONENT_TO_ACTIVE_ROUTE_IN_ROUTE_MAP, state.pastedComponent);
95+
},
7996
// End of componentDisplay Section//////////////////////////////////
8097

8198
// Actions that affect Routing //////////////////////////////////////

src/store/mutations.js

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
breadthFirstSearch,
1313
breadthFirstSearchParent
1414
} from '../utils/search.util'
15+
import _ from 'lodash'
1516

1617
const cloneDeep = require('lodash.clonedeep')
1718

@@ -220,17 +221,70 @@ const mutations = {
220221
},
221222

222223
[types.DELETE_USER_STATE]: (state, payload) => {
223-
// delete state.userState[payload];
224-
// console.log('userState: ', state.userState);
224+
// need to loop through components and take out matching state
225+
for (const component in state.componentMap) {
226+
// first don't go through if component is App or Homeview
227+
if (component === 'App' || component === 'HomeView') continue;
228+
// splice out if there is a match
229+
const newState = state.componentMap[component].state;
230+
const index = newState.indexOf(payload);
231+
if (index > -1) {
232+
newState.splice(index, 1);
233+
state.componentMap[component].state = newState;
234+
} else {
235+
continue;
236+
}
237+
}
238+
// remove from userState
225239
let index = state.userState.indexOf(payload);
226240
state.userState.splice(index, 1);
227241
},
228242

229243
[types.DELETE_USER_ACTIONS]: (state, payload) => {
230-
// payload should be a string of the name of the action to remove
244+
for (const component in state.componentMap) {
245+
// first don't go through if component is App or Homeview
246+
if (component === 'App' || component === 'HomeView') continue;
247+
// splice out if there is a match
248+
const newActions = state.componentMap[component].actions;
249+
const index = newActions.indexOf(payload);
250+
if (index > -1) {
251+
newActions.splice(index, 1);
252+
state.componentMap[component].actions = newActions;
253+
} else {
254+
continue;
255+
}
256+
}
231257
let index = state.userActions.indexOf(payload);
232258
state.userActions.splice(index, 1);
233259
},
260+
// copy and paste functionality
261+
[types.COPY_ACTIVE_COMPONENT]: (state, payload) => {
262+
// copy activeComponentObj and place in the state but without children
263+
const copy = { ...state.activeComponentObj };
264+
copy.componentName;
265+
copy.children = [];
266+
copy.isActive = false;
267+
state.copiedComponent = copy;
268+
// reset the number of copies created
269+
state.copyNumber = 0;
270+
},
271+
272+
[types.PASTE_ACTIVE_COMPONENT]: (state) => {
273+
// check if copied exists
274+
if (state.copiedComponent) {
275+
const { copiedComponent } = state;
276+
// offset duplicate's coordinates
277+
copiedComponent.x += 20;
278+
copiedComponent.y += 20;
279+
const pastedComponent = { ...copiedComponent }
280+
state.componentMap[pastedComponent.componentName += ` (${state.copyNumber})`] = pastedComponent;
281+
282+
// increment copyNumber
283+
state.copyNumber += 1;
284+
// track for pastedComponent
285+
state.pastedComponent = pastedComponent;
286+
}
287+
},
234288

235289
// *** EDIT FUNCTIONALITY *** //////////////////////////////////////////////
236290

@@ -447,7 +501,7 @@ const mutations = {
447501
},
448502

449503
[types.ADD_PARENT]: (state, payload) => {
450-
state.componentMap[payload.componentName].parent[state.parentSelected] = state.componentMap[state.parentSelected]
504+
state.componentMap[payload.componentName].parent[state.parentSelected] = state.componentMap[state.parentSelected];
451505
state.componentMap[state.parentSelected].children.push(
452506
payload.componentName
453507
)
@@ -456,6 +510,14 @@ const mutations = {
456510
)
457511
},
458512

513+
[types.ADD_COPIED_PARENT]: (state, payload) => {
514+
const parentSelected = Object.values(payload.parent)[0].componentName;
515+
// push into parent's children array
516+
state.componentMap[parentSelected].children.push(payload.componentName)
517+
// push into parent's htmlList array
518+
state.componentMap[parentSelected].htmlList.push(payload.componentName)
519+
},
520+
459521
[types.DELETE_ACTIVE_COMPONENT]: state => {
460522
const { componentMap, activeComponent, activeRoute } = state
461523

src/store/state/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ const newState = {
5050
activeTab: 0,
5151
componentChildrenMultiselectValue: [],
5252
modalOpen: false,
53-
parentSelected: false
53+
parentSelected: false,
54+
// for storing copied component
55+
copiedComponent: {},
56+
copyNumber: 0,
57+
pastedComponent: {}
5458
}
5559

5660
// closured method to ensure we only ever write the default state ONCE

src/store/types.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const ADD_COMPONENT_TO_ACTIVE_ROUTE_CHILDREN = 'ADD_COMPONENT_TO_ACTIVE_R
55
export const ADD_COMPONENT_TO_ACTIVE_ROUTE_IN_ROUTE_MAP = 'ADD_COMPONENT_TO_ACTIVE_ROUTE_IN_ROUTE_MAP'
66
export const ADD_COMPONENT_TO_COMPONENT_CHILDREN = 'ADD_COMPONENT_TO_COMPONENT_CHILDREN'
77
export const ADD_COMPONENT_TO_COMPONENT_MAP = 'ADD_COMPONENT_TO_COMPONENT_MAP'
8+
export const ADD_COPIED_PARENT = 'ADD_COPIED_PARENT'
89
export const ADD_NESTED_HTML = 'ADD_NESTED_HTML'
910
export const ADD_NESTED_NO_ACTIVE = 'ADD_NESTED_NO_ACTIVE'
1011
export const ADD_PARENT = 'ADD_PARENT'
@@ -19,6 +20,7 @@ export const ADD_TO_COMPONENT_HTML_LIST = 'ADD_TO_COMPONENT_HTML_LIST'
1920
export const ADD_TO_SELECTED_ELEMENT_LIST = 'ADD_TO_SELECTED_ELEMENT_LIST'
2021
export const CHANGE_ACTIVE_TAB = 'CHANGE_ACTIVE_TAB'
2122
export const CLEAR_IMAGE = 'CLEAR_IMAGE'
23+
export const COPY_ACTIVE_COMPONENT = 'COPY_ACTIVE_COMPONENT'
2224
export const CREATE_ACTION = 'CREATE_ACTION'
2325
export const CREATE_PROP = 'CREATE_PROP'
2426
export const CREATE_STATE = 'CREATE_STATE'
@@ -36,6 +38,7 @@ export const EMPTY_STATE = 'EMPTY_STATE'
3638
export const IMPORT_IMAGE = 'IMPORT_IMAGE'
3739
export const INCREMENT_PROJECT_ID = 'INCREMENT_PROJECT_ID'
3840
export const PARENT_SELECTED = 'PARENT_SELECTED'
41+
export const PASTE_ACTIVE_COMPONENT = 'PASTE_ACTIVE_COMPONENT'
3942
export const SET_ACTIVE_COMPONENT = 'SET_ACTIVE_COMPONENT'
4043
export const SET_ACTIVE_HTML_ELEMENT = 'SET_ACTIVE_HTML_ELEMENT'
4144
export const SET_ACTIVE_LAYER = 'SET_ACTIVE_LAYER'
@@ -75,6 +78,7 @@ export const addToComponentElementList = 'addToComponentElementList'
7578
export const addToSelectedElementList = 'addToSelectedElementList'
7679
export const changeActiveTab = 'changeActiveTab'
7780
export const clearImage = 'clearImage'
81+
export const copyActiveComponent = 'copyActiveComponent'
7882
export const createAction = 'createAction'
7983
export const createProp = 'createProp'
8084
export const createState = 'createState'
@@ -93,6 +97,7 @@ export const importImage = 'importImage'
9397
export const incrementProjectId = 'incrementProjectId'
9498
export const openProject = 'openProject'
9599
export const parentSelected = 'parentSelected'
100+
export const pasteActiveComponent = 'pasteActiveComponent'
96101
export const registerComponent = 'registerComponent'
97102
export const setActiveComponent = 'setActiveComponent'
98103
export const setActiveHTML = 'setActiveHTML'

0 commit comments

Comments
 (0)