components
2 years ago
heading
2 years ago
jetpack-form
2 years ago
lib
2 years ago
list
2 years ago
list-item
2 years ago
paragraph
2 years ago
block-handler.tsx
2 years ago
get-block-handler.tsx
2 years ago
types.ts
2 years ago
with-ai-extension.tsx
2 years ago
block-handler.tsx
96 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import { renderMarkdownFromHTML, renderHTMLFromMarkdown } from '@automattic/jetpack-ai-client'; |
| 5 | import { rawHandler, getBlockContent } from '@wordpress/blocks'; |
| 6 | import { select, dispatch } from '@wordpress/data'; |
| 7 | /** |
| 8 | * Types |
| 9 | */ |
| 10 | import type { BlockBehavior, BlockEditorDispatch, BlockEditorSelect } from './types'; |
| 11 | import type { Block, RenderHTMLRules } from '@automattic/jetpack-ai-client'; |
| 12 | |
| 13 | export function getMarkdown( html: string ) { |
| 14 | return renderMarkdownFromHTML( { content: html } ); |
| 15 | } |
| 16 | |
| 17 | export function renderHTMLContent( markdown: string, rules: RenderHTMLRules = [] ) { |
| 18 | return renderHTMLFromMarkdown( { content: markdown, rules, extension: true } ); |
| 19 | } |
| 20 | |
| 21 | export class BlockHandler { |
| 22 | public clientId: string; |
| 23 | public renderRules: RenderHTMLRules = []; |
| 24 | public firstUpdate: boolean = true; |
| 25 | public behavior: BlockBehavior = 'dropdown' as const; |
| 26 | public isChildBlock: boolean = false; |
| 27 | public feature: string = 'ai-assistant'; |
| 28 | public adjustPosition: boolean = true; |
| 29 | public startOpen: boolean = false; |
| 30 | public hideOnBlockFocus: boolean = true; |
| 31 | |
| 32 | constructor( clientId: string, renderRules: RenderHTMLRules = [] ) { |
| 33 | this.clientId = clientId; |
| 34 | this.renderRules = renderRules; |
| 35 | } |
| 36 | |
| 37 | public getBlock(): Block { |
| 38 | const { getBlock } = select( 'core/block-editor' ) as BlockEditorSelect; |
| 39 | |
| 40 | return getBlock( this.clientId ); |
| 41 | } |
| 42 | |
| 43 | public getContent() { |
| 44 | const block = this.getBlock(); |
| 45 | |
| 46 | return getMarkdown( getBlockContent( block ) ); |
| 47 | } |
| 48 | |
| 49 | public renderContent( markdown: string ) { |
| 50 | return renderHTMLContent( markdown, this.renderRules ); |
| 51 | } |
| 52 | |
| 53 | public onSuggestion( suggestion: string ): void { |
| 54 | // Ignore an empty suggestion |
| 55 | if ( ! suggestion ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | const HTML = this.renderContent( suggestion ); |
| 60 | |
| 61 | this.replaceBlockContent( HTML ); |
| 62 | } |
| 63 | |
| 64 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 65 | public onDone( suggestion: string ): void { |
| 66 | this.firstUpdate = true; |
| 67 | } |
| 68 | |
| 69 | public replaceBlockContent( newContent: string ): void { |
| 70 | // Create a new block with the raw HTML content. |
| 71 | const [ newBlock ] = rawHandler( { HTML: newContent } ); |
| 72 | |
| 73 | if ( ! newBlock ) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | const { updateBlockAttributes, replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } = |
| 78 | dispatch( 'core/block-editor' ) as BlockEditorDispatch; |
| 79 | |
| 80 | // Do not mark the very first change as not persistent. |
| 81 | if ( this.firstUpdate ) { |
| 82 | this.firstUpdate = false; |
| 83 | } else { |
| 84 | // Mark all other changes as not persistent so we can undo all the changes in one step. |
| 85 | __unstableMarkNextChangeAsNotPersistent(); |
| 86 | } |
| 87 | |
| 88 | // Replace the original block attributes with the new block attributes. |
| 89 | updateBlockAttributes( this.clientId, newBlock.attributes ); |
| 90 | |
| 91 | // Replace the original block inner blocks with the new block inner blocks. |
| 92 | __unstableMarkNextChangeAsNotPersistent(); |
| 93 | replaceInnerBlocks( this.clientId, newBlock.innerBlocks ); |
| 94 | } |
| 95 | } |
| 96 |