From 45663f366afe1254f009af9d9a35ee7a6aa33452 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Wed, 23 Oct 2024 17:45:59 +0100 Subject: [PATCH] Rename post to subject --- src/parser/blog-post.ts | 6 +- src/ui/blueprints/EditBlueprint.tsx | 58 +++++++++---------- ...Blueprint.ts => useSubjectForBlueprint.ts} | 22 +++---- 3 files changed, 43 insertions(+), 43 deletions(-) rename src/ui/blueprints/{usePostForBlueprint.ts => useSubjectForBlueprint.ts} (60%) diff --git a/src/parser/blog-post.ts b/src/parser/blog-post.ts index 68870ef..f6f3d08 100644 --- a/src/parser/blog-post.ts +++ b/src/parser/blog-post.ts @@ -4,19 +4,19 @@ import { DateField, newDateField } from '@/model/field/DateField'; import { newTextField, TextField } from '@/model/field/TextField'; import { HtmlField, newHtmlField } from '@/model/field/HtmlField'; -export function parsePostDate( html: string ): DateField { +export function parseBlogPostDate( html: string ): DateField { const container = document.createElement( 'div' ); container.innerHTML = html.trim(); const element = container.querySelector( 'time' ); return newDateField( html, element ? element.dateTime : '' ); } -export function parsePostTitle( html: string ): TextField { +export function parseBlogPostTitle( html: string ): TextField { const deepestChild = findDeepestChild( html ); return newTextField( html, deepestChild?.innerHTML ?? '' ); } -export function parsePostContent( html: string ): HtmlField { +export function parseBlogPostContent( html: string ): HtmlField { return newHtmlField( html, serializeBlocks( html ) ); } diff --git a/src/ui/blueprints/EditBlueprint.tsx b/src/ui/blueprints/EditBlueprint.tsx index 6105fea..52f185b 100644 --- a/src/ui/blueprints/EditBlueprint.tsx +++ b/src/ui/blueprints/EditBlueprint.tsx @@ -5,14 +5,14 @@ import { BlogPostBlueprintEditor } from '@/ui/blueprints/blog-post/BlogPostBluep import { ContentBus } from '@/bus/ContentBus'; import { Toolbar } from '@/ui/blueprints/Toolbar'; import { - parsePostContent, - parsePostDate, - parsePostTitle, + parseBlogPostContent, + parseBlogPostDate, + parseBlogPostTitle, } from '@/parser/blog-post'; import { SubjectType } from '@/model/subject/Subject'; import { Screens } from '@/ui/App'; import { useBlueprint } from '@/ui/blueprints/useBlueprint'; -import { usePostForBlueprint } from '@/ui/blueprints/usePostForBlueprint'; +import { useSubjectForBlueprint } from '@/ui/blueprints/useSubjectForBlueprint'; import { Field } from '@/model/field/Field'; import { BlogPost } from '@/model/subject/BlogPost'; @@ -20,7 +20,7 @@ export function EditBlueprint() { const params = useParams(); const blueprintId = params.blueprintId!; const [ blueprint, setBlueprint ] = useBlueprint( blueprintId ); - const [ post, setPost ] = usePostForBlueprint( blueprint ); + const [ subject, setSubject ] = useSubjectForBlueprint( blueprint ); const { session, apiClient, playgroundClient } = useSessionContext(); const navigate = useNavigate(); @@ -31,12 +31,12 @@ export function EditBlueprint() { } }, [ blueprint ] ); - // Make playground navigate to the post's URL. + // Make playground navigate to the transformed post of the subject. useEffect( () => { - if ( post && !! playgroundClient ) { - void playgroundClient.goTo( '/?p=' + post.transformedId ); + if ( subject && !! playgroundClient ) { + void playgroundClient.goTo( '/?p=' + subject.transformedId ); } - }, [ post, playgroundClient ] ); + }, [ subject, playgroundClient ] ); // Handle field change events. async function onFieldChanged( @@ -44,35 +44,35 @@ export function EditBlueprint() { field: Field, selector: string ) { - if ( ! blueprint || ! post ) { + if ( ! blueprint || ! subject ) { return; } - let postFieldsToUpdate: object | undefined; - switch ( post.type ) { + let subjectFieldsToUpdate: object | undefined; + switch ( subject.type ) { case SubjectType.BlogPost: switch ( name ) { case 'date': - field = parsePostDate( field.original ); - postFieldsToUpdate = { + field = parseBlogPostDate( field.original ); + subjectFieldsToUpdate = { date: field, }; break; case 'title': - field = parsePostTitle( field.original ); - postFieldsToUpdate = { + field = parseBlogPostTitle( field.original ); + subjectFieldsToUpdate = { title: field, }; break; case 'content': - field = parsePostContent( field.original ); - postFieldsToUpdate = { + field = parseBlogPostContent( field.original ); + subjectFieldsToUpdate = { content: field, }; break; } break; default: - throw Error( `unknown post type ${ field.type }` ); + throw Error( `unknown field type ${ field.type }` ); } blueprint.fields[ name ].selector = selector; @@ -88,21 +88,21 @@ export function EditBlueprint() { const bp = await apiClient!.blueprints.update( blueprint ); setBlueprint( bp ); - if ( postFieldsToUpdate ) { + if ( subjectFieldsToUpdate ) { const p = await apiClient!.blogPosts.update( - post.id, - postFieldsToUpdate + subject.id, + subjectFieldsToUpdate ); - setPost( p ); - void playgroundClient.goTo( '/?p=' + post.transformedId ); + setSubject( p ); + void playgroundClient.goTo( '/?p=' + subject.transformedId ); } } let isValid = ! blueprint ? false : blueprint.valid; - if ( ! post ) { + if ( ! subject ) { isValid = false; - } else if ( isValid && post ) { - for ( const f of Object.values( post.fields ) ) { + } else if ( isValid && subject ) { + for ( const f of Object.values( subject.fields ) ) { if ( f.original === '' || f.parsed === '' ) { isValid = false; break; @@ -112,7 +112,7 @@ export function EditBlueprint() { return ( <> - { ! blueprint || ! post ? ( + { ! blueprint || ! subject ? ( 'Loading...' ) : ( <> @@ -131,7 +131,7 @@ export function EditBlueprint() { diff --git a/src/ui/blueprints/usePostForBlueprint.ts b/src/ui/blueprints/useSubjectForBlueprint.ts similarity index 60% rename from src/ui/blueprints/usePostForBlueprint.ts rename to src/ui/blueprints/useSubjectForBlueprint.ts index 9b2cc96..940a00f 100644 --- a/src/ui/blueprints/usePostForBlueprint.ts +++ b/src/ui/blueprints/useSubjectForBlueprint.ts @@ -3,20 +3,20 @@ import { Subject, SubjectType } from '@/model/subject/Subject'; import { useEffect, useState } from 'react'; import { useSessionContext } from '@/ui/session/SessionProvider'; -// Load a post to preview the blueprint's results. -// If a post already exists for the blueprint's source URL, we use that post, -// otherwise we create a new post. -export function usePostForBlueprint( +// Create or load a Subject to preview the Blueprint's results. +// If a Subject already exists for the Blueprint's source URL, we use that Subject, +// otherwise we create a new one. +export function useSubjectForBlueprint( blueprint: Blueprint | undefined -): [ Subject | undefined, ( post: Subject ) => void ] { - const [ post, setPost ] = useState< Subject >(); +): [ Subject | undefined, ( subject: Subject ) => void ] { + const [ subject, setSubject ] = useState< Subject >(); const { apiClient } = useSessionContext(); useEffect( () => { if ( ! blueprint || ! apiClient ) { return; } - async function loadPost( bp: Blueprint ) { + async function loadSubject( bp: Blueprint ) { let p: Subject | null; switch ( bp.type ) { case SubjectType.BlogPost: @@ -33,13 +33,13 @@ export function usePostForBlueprint( } ); break; default: - throw Error( `unknown post type ${ bp.type }` ); + throw Error( `unknown blueprint type ${ bp.type }` ); } } - setPost( p ); + setSubject( p ); } - loadPost( blueprint ).catch( console.error ); + loadSubject( blueprint ).catch( console.error ); }, [ blueprint, apiClient ] ); - return [ post, setPost ]; + return [ subject, setSubject ]; }