選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

page-options.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import * as Dialog from '@radix-ui/react-alert-dialog'
  2. import { MixerVerticalIcon } from '@radix-ui/react-icons'
  3. import {
  4. breakpoints,
  5. IconButton,
  6. DialogOverlay,
  7. DialogContent,
  8. RowButton,
  9. MenuTextInput,
  10. DialogInputWrapper,
  11. Divider,
  12. } from 'components/shared'
  13. import { useEffect, useRef, useState } from 'react'
  14. import state, { useSelector } from 'state'
  15. import { Page } from 'types'
  16. export default function PageOptions({ page }: { page: Page }): JSX.Element {
  17. const [isOpen, setIsOpen] = useState(false)
  18. const rInput = useRef<HTMLInputElement>(null)
  19. const hasOnlyOnePage = useSelector(
  20. (s) => Object.keys(s.data.document.pages).length <= 1
  21. )
  22. const [name, setName] = useState(page.name)
  23. function handleNameChange(e: React.ChangeEvent<HTMLInputElement>) {
  24. setName(e.currentTarget.value)
  25. }
  26. function handleDuplicate() {
  27. state.send('DUPLICATED_PAGE', { id: page.id })
  28. }
  29. function handleDelete() {
  30. state.send('DELETED_PAGE', { id: page.id })
  31. }
  32. function handleOpenChange(isOpen: boolean) {
  33. setIsOpen(isOpen)
  34. if (isOpen) {
  35. return
  36. }
  37. if (page.name.length === 0) {
  38. state.send('RENAMED_PAGE', {
  39. id: page.id,
  40. name: 'Page',
  41. })
  42. }
  43. state.send('SAVED_PAGE_RENAME', { id: page.id })
  44. }
  45. function handleSave() {
  46. state.send('RENAMED_PAGE', {
  47. id: page.id,
  48. name,
  49. })
  50. }
  51. function stopPropagation(e: React.KeyboardEvent<HTMLDivElement>) {
  52. e.stopPropagation()
  53. }
  54. function handleKeydown(e: React.KeyboardEvent<HTMLDivElement>) {
  55. if (e.key === 'Enter') {
  56. handleSave()
  57. setIsOpen(false)
  58. }
  59. }
  60. useEffect(() => {
  61. if (isOpen) {
  62. setTimeout(() => {
  63. rInput.current?.focus()
  64. rInput.current?.select()
  65. }, 0)
  66. }
  67. }, [isOpen])
  68. return (
  69. <Dialog.Root open={isOpen} onOpenChange={handleOpenChange}>
  70. <Dialog.Trigger
  71. as={IconButton}
  72. bp={breakpoints}
  73. size="small"
  74. data-shy="true"
  75. >
  76. <MixerVerticalIcon />
  77. </Dialog.Trigger>
  78. <Dialog.Overlay as={DialogOverlay} />
  79. <Dialog.Content
  80. as={DialogContent}
  81. onKeyDown={stopPropagation}
  82. onKeyUp={stopPropagation}
  83. >
  84. <DialogInputWrapper>
  85. <MenuTextInput
  86. ref={rInput}
  87. value={name}
  88. onChange={handleNameChange}
  89. onKeyDown={handleKeydown}
  90. />
  91. </DialogInputWrapper>
  92. <Divider />
  93. <Dialog.Action
  94. as={RowButton}
  95. bp={breakpoints}
  96. onClick={handleDuplicate}
  97. >
  98. Duplicate
  99. </Dialog.Action>
  100. <Dialog.Action
  101. as={RowButton}
  102. bp={breakpoints}
  103. disabled={hasOnlyOnePage}
  104. onClick={handleDelete}
  105. warn={true}
  106. >
  107. Delete
  108. </Dialog.Action>
  109. <Divider />
  110. <Dialog.Action as={RowButton} bp={breakpoints} onClick={handleSave}>
  111. Save
  112. </Dialog.Action>
  113. <Dialog.Cancel as={RowButton} bp={breakpoints}>
  114. Cancel
  115. </Dialog.Cancel>
  116. </Dialog.Content>
  117. </Dialog.Root>
  118. )
  119. }