編集可能な固定コンポーネント
- ヘッドレスコンテンツを視覚的に編集するユニバーサルエディター。
- ヘッドレスコンテンツをフォームベースで編集するコンテンツフラグメントエディター。
編集可能な React コンポーネントは、「固定」することも、SPA の表示にハードコードすることもできます。 これにより、デベロッパーは SPA エディター互換のコンポーネントを SPA 表示に配置し、ユーザーは AEM SPA エディターでコンポーネントのコンテンツを作成できるようになります。
           
          
この章では、ホームビューのタイトル「Current Adventures」を置き換えます。これは、編集可能な固定タイトルコンポーネントを使用して Home.js でハードコード化されたテキストです。固定コンポーネントはタイトルの配置を保証しますが、タイトルのテキストの作成と開発サイクル外での変更は可能です。
WKND アプリの更新
次のように、固定コンポーネントをホームビューに追加します。
- カスタムの編集可能なタイトルコンポーネントを作成し、プロジェクトのタイトルのリソースタイプに登録します。
- 編集可能なタイトルコンポーネントを SPA ホームビューに配置する
編集可能な React タイトルコンポーネントの作成
SPA ホームビューで、ハードコードされたテキスト <h2>Current Adventures</h2> をカスタムの編集可能なタイトルコンポーネントに置き換えます。タイトルコンポーネントを使用する前に、次の作業を行う必要があります。
- カスタムタイトル React コンポーネントの作成
- 編集可能にする @adobe/aem-react-editable-componentsのメソッドを使用し、カスタムタイトルコンポーネントをデコレートします。
- MapToで編集可能なタイトルコンポーネントを登録して、 後からコンテナコンポーネントで使用できるようにします。
次の手順を実行します。
- 
                  IDE の ~/Code/aem-guides-wknd-graphql/remote-spa-tutorial/react-appでリモート SPA プロジェクトを開く
- 
                  react-app/src/components/editable/core/Title.jsで React コンポーネントを作成
- 
                  Title.jsに次のコードを追加します。code language-javascript import React from 'react' import { RoutedLink } from "./RoutedLink"; const TitleLink = (props) => { return ( <RoutedLink className={props.baseCssClass + (props.nested ? '-' : '__') + 'link'} isRouted={props.routed} to={props.linkURL}> {props.text} </RoutedLink> ); }; const TitleV2Contents = (props) => { if (!props.linkDisabled) { return <TitleLink {...props} /> } return <>{props.text}</> }; export const Title = (props) => { if (!props.baseCssClass) { props.baseCssClass = 'cmp-title' } const elementType = (!!props.type) ? props.type.toString() : 'h3'; return (<div className={props.baseCssClass}> { React.createElement(elementType, { className: props.baseCssClass + (props.nested ? '-' : '__') + 'text', }, <TitleV2Contents {...props} /> ) } </div>) } export const titleIsEmpty = (props) => props.text == null || props.text.trim().length === 0この React コンポーネントは、まだ AEM SPA エディターを使用して編集できないことに注意してください。 この基本コンポーネントは、次の手順で編集可能になります。 実装の詳細については、コードのコメントを参照してください。 
- 
                  react-app/src/components/editable/EditableTitle.jsで React コンポーネントを作成します
- 
                  EditableTitle.jsに次のコードを追加します。code language-javascript // Import the withMappable API provided bu the AEM SPA Editor JS SDK import { EditableComponent, MapTo } from '@adobe/aem-react-editable-components'; import React from 'react' // Import the AEM the Title component implementation and it's Empty Function import { Title, titleIsEmpty } from "./core/Title"; import { withConditionalPlaceHolder } from "./core/util/withConditionalPlaceholder"; import { withStandardBaseCssClass } from "./core/util/withStandardBaseCssClass"; // The sling:resourceType of the AEM component used to collected and serialize the data this React component displays const RESOURCE_TYPE = "wknd-app/components/title"; // Create an EditConfig to allow the AEM SPA Editor to properly render the component in the Editor's context const EditConfig = { emptyLabel: "Title", // The component placeholder in AEM SPA Editor isEmpty: titleIsEmpty, // The function to determine if this component has been authored resourceType: RESOURCE_TYPE // The sling:resourceType this component is mapped to }; export const WrappedTitle = (props) => { const Wrapped = withConditionalPlaceHolder(withStandardBaseCssClass(Title, "cmp-title"), titleIsEmpty, "TitleV2") return <Wrapped {...props} /> } // EditableComponent makes the component editable by the AEM editor, either rendered statically or in a container const EditableTitle = (props) => <EditableComponent config={EditConfig} {...props}><WrappedTitle /></EditableComponent> // MapTo allows the AEM SPA Editor JS SDK to dynamically render components added to SPA Editor Containers MapTo(RESOURCE_TYPE)(EditableTitle); export default EditableTitle;この EditableTitleReact コンポーネントは、TitleReact コンポーネントをラップし、AEM SPA エディターで編集できるようにラップおよびデコレートします。
React 編集可能タイルコンポーネントの使用
編集可能タイル React コンポーネントがReact アプリに登録され、React アプリ内で使用できるようになったので、ホームビューのハードコードされたタイトルテキストを置き換えます。
- 
                  react-app/src/components/Home.jsの編集
- 
                  下部の Home()でEditableTitleを読み込み、ハードコードされたタイトルを新しいAEMTitleコンポーネント:code language-javascript ... import EditableTitle from './editable/EditableTitle'; ... function Home() { return ( <div className="Home"> <EditableTitle pagePath='/content/wknd-app/us/en/home' itemPath='root/title'/> <Adventures /> </div> ); }
Home.js ファイルは次のようになります。
           
          
AEM でのタイトルコンポーネントのオーサリング
- 
                  AEM オーサーにログインします。 
- 
                  サイト/WKND アプリに移動します。 
- 
                  「ホーム」をタップし、上部のアクションバーの「編集」を選択します。 
- 
                  ページエディターの右上にある編集モードセレクターから「編集」を選択します。 
- 
                  青い編集のアウトラインが表示されるまで、WKND ロゴの下、アドベンチャーリストの上にあるデフォルトのタイトルテキストの上にマウスポインターを置きます。 
- 
                  コンポーネントのアクションバーをタップして表示し、レンチをタップして 編集します。   
- 
                  タイトルコンポーネントを作成します。 - 
                      タイトル: WKND アドベンチャー 
- 
                      種類/サイズ:H2   
 
- 
                      
- 
                  「完了」をタップして保存します。 
- 
                  AEM SPA エディターで変更をプレビューします。 
- 
                  http://localhost:3000 でローカルに実行している WKND アプリを更新し、作成したタイトルの変更が直ちに反映されることを確認します。   
おめでとうございます。
WKND アプリに編集可能な固定コンポーネントが追加されました。 次の方法を学習しました。
- SPA への固定(ただし編集可能)コンポーネントの作成
- AEM での固定コンポーネントの作成
- 作成したコンテンツのリモート SPA での確認
次の手順
次は、SPA に作成者が編集可能なコンポーネントを追加できるようにする AEM ResponsiveGrid コンテナコンポーネントを SPA に追加します。