-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
331 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/components/DynamicObject/DynamicObjectForm/DynamicField/extensions/area.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Mark, mergeAttributes } from '@tiptap/core' | ||
|
||
declare module '@tiptap/core' { | ||
interface Commands<ReturnType> { | ||
area: { | ||
setArea: (attributes: { | ||
'data-gebiedengroep': string | ||
'data-type': string | ||
'data-gebiedsaanwijzing': string | ||
text?: string | ||
}) => ReturnType | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This extension allows you to insert areas. | ||
*/ | ||
export const Area = Mark.create({ | ||
name: 'area', | ||
|
||
addOptions() { | ||
return { | ||
HTMLAttributes: { | ||
href: '#', | ||
'data-gebiedengroep': null, | ||
'data-type': null, | ||
'data-gebiedsaanwijzing': null, | ||
}, | ||
} | ||
}, | ||
|
||
addAttributes() { | ||
return { | ||
href: { | ||
default: this.options.HTMLAttributes.href, | ||
}, | ||
'data-gebiedengroep': { | ||
default: this.options.HTMLAttributes['data-gebiedengroep'], | ||
}, | ||
'data-type': { | ||
default: this.options.HTMLAttributes['data-type'], | ||
}, | ||
'data-gebiedsaanwijzing': { | ||
default: this.options.HTMLAttributes['data-gebiedsaanwijzing'], | ||
}, | ||
} | ||
}, | ||
|
||
renderHTML({ HTMLAttributes }) { | ||
return [ | ||
'a', | ||
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), | ||
0, | ||
] | ||
}, | ||
|
||
addCommands() { | ||
return { | ||
setArea: | ||
attributes => | ||
({ chain, state }) => { | ||
const { empty } = state.selection | ||
|
||
if (empty) { | ||
const { text = 'default text', ...rest } = attributes | ||
return chain() | ||
.insertContentAt(state.selection.anchor, [ | ||
{ | ||
type: 'text', | ||
text, | ||
marks: [ | ||
{ | ||
type: this.name, | ||
attrs: rest, | ||
}, | ||
], | ||
}, | ||
]) | ||
.run() | ||
} else { | ||
return chain().setMark(this.name, attributes).run() | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
src/components/Modals/ObjectModals/ObjectAreaAnnotateModal/ObjectAreaAnnotateModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { Button, FormikSelect } from '@pzh-ui/components' | ||
import { Form, Formik } from 'formik' | ||
import { useMemo } from 'react' | ||
import { z } from 'zod' | ||
import { toFormikValidationSchema } from 'zod-formik-adapter' | ||
|
||
import Modal from '@/components/Modal' | ||
import useModalStore from '@/store/modalStore' | ||
import { SCHEMA_OBJECT_ANNOTATE_AREA } from '@/validation/objectAnnotate' | ||
|
||
import { ModalStateMap } from '../../types' | ||
|
||
type Values = z.infer<typeof SCHEMA_OBJECT_ANNOTATE_AREA> | ||
|
||
const ObjectAreaAnnotateModal = () => { | ||
const setActiveModal = useModalStore(state => state.setActiveModal) | ||
const modalState = useModalStore( | ||
state => state.modalStates['objectAreaAnnotate'] | ||
) as ModalStateMap['objectAreaAnnotate'] | ||
|
||
const groupOptions = [ | ||
{ | ||
label: 'Gebiedengroep 1', | ||
value: 'group-1', | ||
}, | ||
] | ||
|
||
const areaTypeOptions = [{ label: 'Type 1', value: 'type-1' }] | ||
|
||
const areaGroupOptions = [ | ||
{ | ||
label: 'Gebiedengroep 1', | ||
value: 'group-1', | ||
}, | ||
] | ||
|
||
const isEmptySelection = useMemo( | ||
() => modalState?.editor.state.selection.empty, | ||
[modalState?.editor.state.selection.empty] | ||
) | ||
|
||
const hasValues = useMemo( | ||
() => { | ||
const values = modalState?.editor?.getAttributes('area') | ||
|
||
return !!values && !!Object.keys(values).length | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[modalState?.editor, modalState?.editor?.state.selection] | ||
) | ||
|
||
const initialValues: Values = useMemo(() => { | ||
const previousValues = modalState?.editor?.getAttributes('area') | ||
|
||
return { | ||
group: previousValues?.['data-gebiedengroep'] ?? '', | ||
areaType: previousValues?.['data-type'] ?? '', | ||
areaGroup: previousValues?.['data-gebiedsaanwijzing'] ?? '', | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [modalState?.editor, modalState?.editor?.state.selection]) | ||
|
||
const handleSubmit = (payload: Values) => { | ||
const groupName = groupOptions.find( | ||
option => option.value === payload.group | ||
)?.label | ||
|
||
modalState?.editor | ||
?.chain() | ||
.focus() | ||
.extendMarkRange('area') | ||
.setArea({ | ||
'data-gebiedengroep': payload.group, | ||
'data-type': payload.areaType, | ||
'data-gebiedsaanwijzing': payload.areaGroup, | ||
text: isEmptySelection ? groupName : undefined, | ||
}) | ||
.run() | ||
|
||
setActiveModal(null) | ||
} | ||
|
||
return ( | ||
<Modal | ||
id="objectAreaAnnotate" | ||
title="Gebiedsaanwijzing toevoegen" | ||
size="m"> | ||
<Formik | ||
onSubmit={handleSubmit} | ||
initialValues={initialValues} | ||
enableReinitialize | ||
validationSchema={toFormikValidationSchema( | ||
SCHEMA_OBJECT_ANNOTATE_AREA | ||
)}> | ||
{({ isValid, isSubmitting, dirty }) => ( | ||
<Form> | ||
<div className="space-y-4"> | ||
<div> | ||
<FormikSelect | ||
name="group" | ||
label="Gebiedengroep" | ||
placeholder="Selecteer een gebiedengroep" | ||
options={groupOptions} | ||
required | ||
/> | ||
</div> | ||
<div> | ||
<FormikSelect | ||
name="areaType" | ||
label="Type gebiedsaanwijzing" | ||
placeholder="Selecteer een type gebiedsaanwijzing" | ||
options={areaTypeOptions} | ||
required | ||
/> | ||
</div> | ||
<div> | ||
<FormikSelect | ||
name="areaGroup" | ||
label="Gebiedsaanwijzinggroep" | ||
placeholder="Selecteer een gebiedsaanwijzinggroep" | ||
options={areaGroupOptions} | ||
required | ||
/> | ||
</div> | ||
</div> | ||
|
||
<div className="mt-6 flex items-center justify-between"> | ||
{!hasValues ? ( | ||
<Button | ||
variant="link" | ||
onPress={() => setActiveModal(null)}> | ||
Annuleren | ||
</Button> | ||
) : ( | ||
<Button | ||
variant="link" | ||
onPress={() => { | ||
modalState?.editor | ||
?.chain() | ||
.focus() | ||
.extendMarkRange('area') | ||
.unsetMark('area') | ||
.run() | ||
setActiveModal(null) | ||
}} | ||
className="text-pzh-red-500"> | ||
Gebiedsaanwijzing verwijderen | ||
</Button> | ||
)} | ||
<Button | ||
variant="cta" | ||
type="submit" | ||
isDisabled={!isValid || isSubmitting || !dirty} | ||
isLoading={isSubmitting}> | ||
Opslaan | ||
</Button> | ||
</div> | ||
</Form> | ||
)} | ||
</Formik> | ||
</Modal> | ||
) | ||
} | ||
|
||
export default ObjectAreaAnnotateModal |
1 change: 1 addition & 0 deletions
1
src/components/Modals/ObjectModals/ObjectAreaAnnotateModal/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './ObjectAreaAnnotateModal' |
Oops, something went wrong.