DocumentPage.hooks.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import { useEffect, useState } from 'react';
  2. import {
  3. DocumentEventGetDocument,
  4. DocumentVersionPB,
  5. OpenDocumentPayloadPB,
  6. } from '../../services/backend/events/flowy-document';
  7. import { BlockInterface, BlockType } from '../interfaces';
  8. import { useParams } from 'react-router-dom';
  9. import { getBlockEditor, createBlockEditor } from '../block_editor';
  10. const loadBlockData = async (id: string): Promise<Record<string, BlockInterface>> => {
  11. return {
  12. [id]: {
  13. id: id,
  14. type: BlockType.PageBlock,
  15. data: { title: 'Document Title' },
  16. next: null,
  17. firstChild: "L1-1",
  18. },
  19. "L1-1": {
  20. id: "L1-1",
  21. type: BlockType.HeadingBlock,
  22. data: { level: 1, content: [{ text: 'Heading 1' }] },
  23. next: "L1-2",
  24. firstChild: null,
  25. },
  26. "L1-2": {
  27. id: "L1-2",
  28. type: BlockType.HeadingBlock,
  29. data: { level: 2, content: [{ text: 'Heading 2' }] },
  30. next: "L1-3",
  31. firstChild: null,
  32. },
  33. "L1-3": {
  34. id: "L1-3",
  35. type: BlockType.HeadingBlock,
  36. data: { level: 3, content: [{ text: 'Heading 3' }] },
  37. next: "L1-4",
  38. firstChild: null,
  39. },
  40. "L1-4": {
  41. id: "L1-4",
  42. type: BlockType.TextBlock,
  43. data: { content: [
  44. {
  45. text:
  46. 'This example shows how you can make a hovering menu appear above your content, which you can use to make text ',
  47. },
  48. { text: 'bold', bold: true },
  49. { text: ', ' },
  50. { text: 'italic', italic: true },
  51. { text: ', or anything else you might want to do!' },
  52. ] },
  53. next: "L1-5",
  54. firstChild: null,
  55. },
  56. "L1-5": {
  57. id: "L1-5",
  58. type: BlockType.TextBlock,
  59. data: { content: [
  60. { text: 'Try it out yourself! Just ' },
  61. { text: 'select any piece of text and the menu will appear', bold: true },
  62. { text: '.' },
  63. ] },
  64. next: "L1-6",
  65. firstChild: "L1-5-1",
  66. },
  67. "L1-5-1": {
  68. id: "L1-5-1",
  69. type: BlockType.TextBlock,
  70. data: { content: [
  71. { text: 'Try it out yourself! Just ' },
  72. ] },
  73. next: "L1-5-2",
  74. firstChild: null,
  75. },
  76. "L1-5-2": {
  77. id: "L1-5-2",
  78. type: BlockType.TextBlock,
  79. data: { content: [
  80. { text: 'Try it out yourself! Just ' },
  81. ] },
  82. next: null,
  83. firstChild: null,
  84. },
  85. "L1-6": {
  86. id: "L1-6",
  87. type: BlockType.ListBlock,
  88. data: { type: 'bulleted', content: [
  89. {
  90. text:
  91. "Since it's rich text, you can do things like turn a selection of text ",
  92. },
  93. { text: 'bold', bold: true },
  94. {
  95. text:
  96. ', or add a semantically rendered block quote in the middle of the page, like this:',
  97. },
  98. ] },
  99. next: "L1-7",
  100. firstChild: "L1-6-1",
  101. },
  102. "L1-6-1": {
  103. id: "L1-6-1",
  104. type: BlockType.ListBlock,
  105. data: { type: 'numbered', content: [
  106. {
  107. text:
  108. "Since it's rich text, you can do things like turn a selection of text ",
  109. },
  110. ] },
  111. next: "L1-6-2",
  112. firstChild: null,
  113. },
  114. "L1-6-2": {
  115. id: "L1-6-2",
  116. type: BlockType.ListBlock,
  117. data: { type: 'numbered', content: [
  118. {
  119. text:
  120. "Since it's rich text, you can do things like turn a selection of text ",
  121. },
  122. ] },
  123. next: "L1-6-3",
  124. firstChild: null,
  125. },
  126. "L1-6-3": {
  127. id: "L1-6-3",
  128. type: BlockType.TextBlock,
  129. data: { content: [{ text: 'A wise quote.' }] },
  130. next: null,
  131. firstChild: null,
  132. },
  133. "L1-7": {
  134. id: "L1-7",
  135. type: BlockType.ListBlock,
  136. data: { type: 'column' },
  137. next: "L1-8",
  138. firstChild: "L1-7-1",
  139. },
  140. "L1-7-1": {
  141. id: "L1-7-1",
  142. type: BlockType.ColumnBlock,
  143. data: { ratio: '0.33' },
  144. next: "L1-7-2",
  145. firstChild: "L1-7-1-1",
  146. },
  147. "L1-7-1-1": {
  148. id: "L1-7-1-1",
  149. type: BlockType.TextBlock,
  150. data: { content: [
  151. { text: 'Try it out yourself! Just ' },
  152. ] },
  153. next: null,
  154. firstChild: null,
  155. },
  156. "L1-7-2": {
  157. id: "L1-7-2",
  158. type: BlockType.ColumnBlock,
  159. data: { ratio: '0.33' },
  160. next: "L1-7-3",
  161. firstChild: "L1-7-2-1",
  162. },
  163. "L1-7-2-1": {
  164. id: "L1-7-2-1",
  165. type: BlockType.TextBlock,
  166. data: { content: [
  167. { text: 'Try it out yourself! Just ' },
  168. ] },
  169. next: "L1-7-2-2",
  170. firstChild: null,
  171. },
  172. "L1-7-2-2": {
  173. id: "L1-7-2-2",
  174. type: BlockType.TextBlock,
  175. data: { content: [
  176. { text: 'Try it out yourself! Just ' },
  177. ] },
  178. next: null,
  179. firstChild: null,
  180. },
  181. "L1-7-3": {
  182. id: "L1-7-3",
  183. type: BlockType.ColumnBlock,
  184. data: { ratio: '0.33' },
  185. next: null,
  186. firstChild: "L1-7-3-1",
  187. },
  188. "L1-7-3-1": {
  189. id: "L1-7-3-1",
  190. type: BlockType.TextBlock,
  191. data: { content: [
  192. { text: 'Try it out yourself! Just ' },
  193. ] },
  194. next: null,
  195. firstChild: null,
  196. },
  197. }
  198. }
  199. export const useDocument = () => {
  200. const params = useParams();
  201. const [blockId, setBlockId] = useState<string>();
  202. const loadDocument = async (id: string): Promise<any> => {
  203. const getDocumentResult = await DocumentEventGetDocument(
  204. OpenDocumentPayloadPB.fromObject({
  205. document_id: id,
  206. version: DocumentVersionPB.V1,
  207. })
  208. );
  209. if (getDocumentResult.ok) {
  210. const pb = getDocumentResult.val;
  211. return JSON.parse(pb.content);
  212. } else {
  213. throw new Error('get document error');
  214. }
  215. };
  216. useEffect(() => {
  217. void (async () => {
  218. if (!params?.id) return;
  219. const data = await loadBlockData(params.id);
  220. console.log('==== enter ====', params?.id, data);
  221. const blockEditor = getBlockEditor();
  222. if (blockEditor) {
  223. blockEditor.changeDoc(params?.id, data);
  224. } else {
  225. createBlockEditor(params?.id, data);
  226. }
  227. setBlockId(params.id)
  228. })();
  229. return () => {
  230. console.log('==== leave ====', params?.id)
  231. }
  232. }, [params.id]);
  233. return { blockId };
  234. };