5454</template >
5555
5656<script lang="ts">
57- import { defineComponent , ref } from ' vue'
57+ import { defineComponent , nextTick , ref } from ' vue'
5858import Stepper from ' components/Stepper.vue'
5959import StepperActions from ' components/StepperActions.vue'
6060import IdentifierCardEditing from ' components/IdentifierCardEditing.vue'
6161import IdentifierCardViewing from ' components/IdentifierCardViewing.vue'
6262import { IdentifierType , IdentifierTypeType } from ' src/types'
6363import { useCff } from ' src/store/cff'
64+ import { scrollToBottom } from ' ../scroll-to-bottom'
6465
6566export default defineComponent ({
6667 name: ' ScreenIdentifiers' ,
@@ -73,47 +74,51 @@ export default defineComponent({
7374 setup () {
7475 const { identifiers, setIdentifiers } = useCff ()
7576 const editingId = ref (- 1 )
77+ const addIdentifier = async () => {
78+ const newIdentifier: IdentifierType = {
79+ type: ' doi' ,
80+ value: ' ' ,
81+ description: ' '
82+ }
83+ const newIdentifiers = [... identifiers .value , newIdentifier ]
84+ setIdentifiers (newIdentifiers )
85+ editingId .value = newIdentifiers .length - 1
86+ // await the DOM update that resulted from updating the identifiers list
87+ await nextTick ()
88+ scrollToBottom ()
89+ }
90+ const removeIdentifier = () => {
91+ const newIdentifiers = [... identifiers .value ]
92+ newIdentifiers .splice (editingId .value , 1 )
93+ setIdentifiers (newIdentifiers )
94+ editingId .value = - 1
95+ }
96+ const setIdentifierDescriptionField = (field : keyof IdentifierType , value : string ) => {
97+ const identifier = { ... identifiers .value [editingId .value ] }
98+ identifier .description = value
99+ identifiers .value [editingId .value ] = identifier
100+ setIdentifiers (identifiers .value )
101+ }
102+ const setIdentifierTypeField = (field : keyof IdentifierType , value : IdentifierTypeType ) => {
103+ const identifier = { ... identifiers .value [editingId .value ] }
104+ identifier .type = value
105+ identifiers .value [editingId .value ] = identifier
106+ setIdentifiers (identifiers .value )
107+ }
108+ const setIdentifierValueField = (field : keyof IdentifierType , value : string ) => {
109+ const identifier = { ... identifiers .value [editingId .value ] }
110+ identifier .value = value
111+ identifiers .value [editingId .value ] = identifier
112+ setIdentifiers (identifiers .value )
113+ }
76114 return {
77- identifiers ,
115+ addIdentifier ,
78116 editingId ,
79- setIdentifierTypeField (field : keyof IdentifierType , value : IdentifierTypeType ) {
80- const identifier = { ... identifiers .value [editingId .value ] }
81- identifier .type = value
82- identifiers .value [editingId .value ] = identifier
83- setIdentifiers (identifiers .value )
84- },
85- setIdentifierValueField (field : keyof IdentifierType , value : string ) {
86- const identifier = { ... identifiers .value [editingId .value ] }
87- identifier .value = value
88- identifiers .value [editingId .value ] = identifier
89- setIdentifiers (identifiers .value )
90- },
91- setIdentifierDescriptionField (field : keyof IdentifierType , value : string ) {
92- const identifier = { ... identifiers .value [editingId .value ] }
93- identifier .description = value
94- identifiers .value [editingId .value ] = identifier
95- setIdentifiers (identifiers .value )
96- },
97- removeIdentifier () {
98- const newIdentifiers = [... identifiers .value ]
99- newIdentifiers .splice (editingId .value , 1 )
100- setIdentifiers (newIdentifiers )
101- editingId .value = - 1
102- },
103- addIdentifier () {
104- const newIdentifier: IdentifierType = {
105- type: ' doi' ,
106- value: ' ' ,
107- description: ' '
108- }
109- const newIdentifiers = [... identifiers .value , newIdentifier ]
110- setIdentifiers (newIdentifiers )
111- editingId .value = newIdentifiers .length - 1
112- setTimeout (() => {
113- // FIXME shouldn't have to use a timeout but it seems the DOM doesn't update in time
114- document .getElementsByClassName (' bottom' )[0 ].scrollIntoView ({ behavior: ' smooth' })
115- }, 100 )
116- }
117+ identifiers ,
118+ removeIdentifier ,
119+ setIdentifierDescriptionField ,
120+ setIdentifierTypeField ,
121+ setIdentifierValueField
117122 }
118123 }
119124})
0 commit comments