-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from contentstack/EB-10-handling-empty-blocks
Introduced `EmptyBlocks` for handling empty block entries
- Loading branch information
Showing
5 changed files
with
165 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { CslpData } from "../../cslp/types/cslp.types"; | ||
import liveEditorPostMessage from "../utils/liveEditorPostMessage"; | ||
import { ISchemaFieldMap } from "../utils/types/index.types"; | ||
import { LiveEditorPostMessageEvents } from "../utils/types/postMessage.types"; | ||
|
||
interface EmptyBlockProps { | ||
details: { | ||
fieldMetadata: CslpData; | ||
fieldSchema: ISchemaFieldMap; | ||
} | ||
} | ||
|
||
export function EmptyBlock(props: EmptyBlockProps) { | ||
|
||
const { details } = props | ||
|
||
const blockParentName = details.fieldSchema.display_name; | ||
|
||
function sendAddInstanceEvent() { | ||
liveEditorPostMessage?.send( | ||
LiveEditorPostMessageEvents.ADD_INSTANCE, | ||
{ | ||
fieldMetadata: details.fieldMetadata, | ||
index: 0, | ||
} | ||
); | ||
} | ||
|
||
return ( | ||
<div className="visual-editor__empty-block"> | ||
<div className="visual-editor__empty-block-title"> | ||
There are no {blockParentName.toLowerCase()} to show in this section. | ||
</div> | ||
<button className={'visual-editor__empty-block-add-button'} onClick={() => sendAddInstanceEvent()}> | ||
<i className="fas fa-plus"></i> | ||
{blockParentName} | ||
</button> | ||
</div>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { hydrate } from "preact"; | ||
import { EmptyBlock } from "../components/emptyBlock"; | ||
import { extractDetailsFromCslp } from "../../cslp"; | ||
import { FieldSchemaMap } from "../utils/fieldSchemaMap"; | ||
|
||
|
||
export async function generateEmptyBlocks( | ||
emptyBlockParents: Element[] | [] | ||
): Promise<void> { | ||
|
||
for (const emptyBlockParent of emptyBlockParents) { | ||
|
||
const cslpData = emptyBlockParent.getAttribute("data-cslp"); | ||
if (!cslpData) { | ||
return; | ||
} | ||
const fieldMetadata = extractDetailsFromCslp(cslpData); | ||
|
||
const fieldSchema = await FieldSchemaMap.getFieldSchema( | ||
fieldMetadata.content_type_uid, | ||
fieldMetadata.fieldPath | ||
); | ||
|
||
hydrate(<EmptyBlock details={{ | ||
fieldSchema, | ||
fieldMetadata | ||
}}/>, emptyBlockParent); | ||
|
||
} | ||
} | ||
|
||
export function removeEmptyBlocks( | ||
emptyBlockParents: Element[] | [] | ||
): void { | ||
emptyBlockParents?.forEach((emptyBlockParent) => { | ||
|
||
const emptyBlock = emptyBlockParent.querySelector(".visual-editor__empty-block"); | ||
|
||
if (emptyBlock) { | ||
emptyBlock.remove(); | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters