Skip to content

Commit

Permalink
Rename post to subject
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Oct 23, 2024
1 parent 3c3cff1 commit 45663f3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/parser/blog-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}

Expand Down
58 changes: 29 additions & 29 deletions src/ui/blueprints/EditBlueprint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ 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';

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();

Expand All @@ -31,48 +31,48 @@ 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(
name: string,
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;
Expand All @@ -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;
Expand All @@ -112,7 +112,7 @@ export function EditBlueprint() {

return (
<>
{ ! blueprint || ! post ? (
{ ! blueprint || ! subject ? (
'Loading...'
) : (
<>
Expand All @@ -131,7 +131,7 @@ export function EditBlueprint() {
</Toolbar>
<BlogPostBlueprintEditor
blueprint={ blueprint }
subject={ post as BlogPost }
subject={ subject as BlogPost }
onFieldChanged={ onFieldChanged }
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 ];
}

0 comments on commit 45663f3

Please sign in to comment.