({ children }: CanvasProviderProps)
| 29 | const CanvasContext = createContext<CanvasContextData>({} as CanvasContextData); |
| 30 | |
| 31 | const CanvasProvider = ({ children }: CanvasProviderProps) => { |
| 32 | const [sections, setSections] = usePersistedState<CanvasSection[]>( |
| 33 | 'canvas.sections', |
| 34 | [] |
| 35 | ); |
| 36 | |
| 37 | const [currentSection, setCurrentSection] = useState<CanvasSection>(); |
| 38 | const [previewTemplate, setPreviewTemplate] = useState<CanvasSection[]>([]); |
| 39 | |
| 40 | const { extensions } = useExtensions(); |
| 41 | |
| 42 | const handleAddSection = (event: HandleAddSectionArgs) => { |
| 43 | const sectionType = event.detail; |
| 44 | |
| 45 | const sectionData = extensions.sections[ |
| 46 | sectionType |
| 47 | ] as CanvasSection['props']; |
| 48 | |
| 49 | const newSection = { |
| 50 | id: uuid(), |
| 51 | type: event.detail, |
| 52 | ...sectionData.defaultConfig, |
| 53 | }; |
| 54 | |
| 55 | setSections(state => [...state, newSection]); |
| 56 | }; |
| 57 | |
| 58 | const loadReadmeFile = async () => { |
| 59 | document.getElementById('readme-file-import')?.click(); |
| 60 | }; |
| 61 | |
| 62 | const importReadme = async (event: ImportReadme) => { |
| 63 | const file = event?.detail?.target?.files?.[0]; |
| 64 | if (!file) return; // TODO: toast error event |
| 65 | const text = await file.text(); |
| 66 | const sections = await parseImportedReadme(text); |
| 67 | handleClearCanvas(); |
| 68 | setSections(sections); |
| 69 | }; |
| 70 | |
| 71 | const handleEditSection = (event: HandleEditSectionArgs) => { |
| 72 | const { id = currentSection?.id, path, value } = event.detail; |
| 73 | |
| 74 | if (!id) return; |
| 75 | |
| 76 | const obj = sections.find(item => item.id === id)!; |
| 77 | |
| 78 | const result = deepChangeObjectProperty<CanvasSection>({ |
| 79 | obj, |
| 80 | path, |
| 81 | value, |
| 82 | }); |
| 83 | |
| 84 | setSections(state => |
| 85 | state.map(item => { |
| 86 | if (item.id === id) return result; |
| 87 | return item; |
| 88 | }) |
nothing calls this directly
no test coverage detected