リンク内容の取得

このサンプルではコピーするリンクの内容取得機能を確認することができます。

コメントコンポーネントのリンクをコピーして別のコメントコンポーネントに貼り付ける場合のリンク内容を取得する機能について解説します。 リンク内容 リンクの表示形式をInlineまたはCard形式を設定する場合、別のコメントコンポーネントにリンクを貼り付けると、loadCommentByLinkプロパティを使用して、リンク内容を取得する必要があります。 内容が取得できない場合、リンクの状態をアクセス拒否や存在しないに設定できます。設定方法はloadStateプロパティにLoadCommentByLinkState列挙体を指定します。 設定例は以下の通りです。
import * as React from "react"; import * as ReactDom from "react-dom"; import { GcComment } from "@mescius/inputman.comment.react"; import { InputMan } from "@mescius/inputman.comment"; import "@mescius/inputman.richtexteditor"; import { postedComments } from './data'; import '@mescius/inputman.comment/CSS/gc.inputman.comment.css'; import '@mescius/inputman.richtexteditor/CSS/gc.inputman.richtexteditor.css'; import './styles.css'; const App = () => { const gcCommentProps = { pastedLinkType: InputMan.GcCommentPasteLinkType.Inline, copiedLinkDisplayTextFormatter: (comment) => { return { linkText: `${comment.userInfo.name}: ${comment.content}`, infoText: `${comment.postTime.toLocaleString('ja-JP', { dateStyle: 'long', timeStyle: 'short', })} に送信`, }; }, copiedLinkSearchParams: [ InputMan.GcCommentLinkSearchParams.ComponentId, InputMan.GcCommentLinkSearchParams.CommentId, InputMan.GcCommentLinkSearchParams.UpdateTime, InputMan.GcCommentLinkSearchParams.UserId, ], editorConfig: { editorType: InputMan.GcCommentEditorType.GcRichTextEditor, menubar: [], toolbar: [], height: 150, }, userInfo: { id: '1', name: '森上 偉久馬', avatar: '$IMDEMOROOT$/ja/samples/comment/copylink/loadComment/img/avatar1.png', }, loadComments: (args) => { return { comments: [postedComments[0]], }; }, header: [], loadCommentByLink: (link) => { return { comment: null, loadState: InputMan.LoadCommentByLinkState.NotExist, }; }, }; const gcComment2Props = { pastedLinkType: InputMan.GcCommentPasteLinkType.Card, copiedLinkDisplayTextFormatter: (comment) => { return { linkText: `${comment.userInfo.name}: ${comment.content}`, infoText: `${comment.postTime.toLocaleString('ja-JP', { dateStyle: 'long', timeStyle: 'short', })} に送信`, }; }, copiedLinkSearchParams: [ InputMan.GcCommentLinkSearchParams.ComponentId, InputMan.GcCommentLinkSearchParams.CommentId, ], editorConfig: { editorType: InputMan.GcCommentEditorType.GcRichTextEditor, menubar: [], toolbar: [], height: 150, }, userInfo: { id: '2', name: '葛城 孝史', avatar: '$IMDEMOROOT$/ja/samples/comment/copylink/loadComment/img/avatar2.png', }, loadComments: (args) => { return { comments: [postedComments[1]], }; }, header: [], loadCommentByLink: (link) => { const url = new URL(link); const commentId = url.searchParams.get('commentId'); const componentId = url.searchParams.get('componentId'); let comment; if (componentId === gcCommentRef.current.getNestedIMControl().getElement().id) { comment = gcCommentRef.current.getNestedIMControl().comments.find((c) => c.id === commentId); } else { comment = gcComment2Ref.current.getNestedIMControl().comments.find((c) => c.id === commentId); } return { comment, loadState: comment ? InputMan.LoadCommentByLinkState.Success : InputMan.LoadCommentByLinkState.NotExist, }; }, beforeExecuteCommand: (e) => { if (e.command === InputMan.GcCommentCommand.OpenCommentLink) { e.preventDefault(); const url = new URL(e.value.url); const componentId = url.searchParams.get('componentId'); const commentId = url.searchParams.get('commentId'); if (componentId === 'GcComment_0') { gcCommentRef.current.getNestedIMControl().execCommand(InputMan.GcCommentCommand.GotoComment, { commentId: commentId, }); } else if (componentId === 'GcComment_1') { gcComment2Ref.current.getNestedIMControl().execCommand(InputMan.GcCommentCommand.GotoComment, { commentId: commentId, }); } } } }; const gcCommentRef = React.createRef(); const gcComment2Ref = React.createRef(); return ( <div> <b>こちらのコメントエディタに貼り付けると、「存在しません」エラーが発生します。</b> <GcComment openLinkMode={InputMan.OpenLinkMode.GoToComment} ref={gcCommentRef} {...gcCommentProps}></GcComment> <br/> <b>こちらのコメントエディタに貼り付けると、コメントが表示されます。</b> <GcComment openLinkMode={InputMan.OpenLinkMode.GoToComment} ref={gcComment2Ref} {...gcComment2Props}></GcComment> </div> ); }; ReactDom.render(<App />, document.getElementById("app"));
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta name="description" content="" lang="ja" xml:lang="ja" /> <title>コメントコンポーネント - リンクのコピー</title> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> window.onload = function() { System.import('./src/app'); } </script> </head> <body> <div id="app"></div> </body> </html>
export const postedComments = [ { id: '1', userInfo: { id: '1', name: '森上 偉久馬', avatar: '$IMDEMOROOT$/ja/samples/comment/copylink/loadComment/img/avatar1.png', }, content: 'このコメントのリンクをコピーして、下のコメントエディタにペーストすると、問題なく内容が表示されます。', postTime: new Date(2024, 1, 1, 8, 0, 0), updateTime: new Date(2024, 1, 1, 8, 0, 0), reactions: [ { reactionChar: '👍', count: 2, }, ], }, { id: '2', userInfo: { id: '5', name: '松沢 誠一', avatar: '$IMDEMOROOT$/ja/samples/comment/copylink/loadComment/img/avatar5.png', }, content: 'このコメントのリンクをコピーして、上のコメントエディタにペーストすると、「存在しません」エラーが発生します。', postTime: new Date(2024, 7, 23, 8, 1, 59), updateTime: new Date(2024, 7, 23, 8, 1, 59), reactions: [ { reactionChar: '👍', count: 6, currentUserReacted: true, }, ], }, ];
body { box-sizing: content-box !important; height: 551px !important; } .gcim__gccomment { border: 1px solid #ccc; border-radius: 4px; padding: 10px; box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1); }
(function (global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true, react: true, }, meta: { '*.css': { loader: 'css' }, }, paths: { // paths serve as alias 'npm:': 'node_modules/', }, // map tells the System loader where to look for things map: { '@mescius/inputman': 'npm:@mescius/inputman/index.js', '@mescius/inputman.react': 'npm:@mescius/inputman.react/GcInputMan.component.js', '@mescius/inputman/CSS': 'npm:@mescius/inputman/CSS', '@mescius/inputman.comment': 'npm:@mescius/inputman.comment/index.js', '@mescius/inputman.comment.react': 'npm:@mescius/inputman.comment.react/GcInputMan.component.js', '@mescius/inputman.comment/CSS': 'npm:@mescius/inputman.comment/CSS', '@mescius/inputman.richtexteditor': 'npm:@mescius/inputman.richtexteditor/index.js', '@mescius/inputman.richtexteditor.react': 'npm:@mescius/inputman.richtexteditor.react/GcInputMan.component.js', '@mescius/inputman.richtexteditor/CSS': 'npm:@mescius/inputman.richtexteditor/CSS', '@mescius/inputman.richtexteditor/JS/plugins/advlist': 'npm:@mescius/inputman.richtexteditor/JS/plugins/advlist/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/all': 'npm:@mescius/inputman.richtexteditor/JS/plugins/all/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/autosave': 'npm:@mescius/inputman.richtexteditor/JS/plugins/autosave/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/charmap': 'npm:@mescius/inputman.richtexteditor/JS/plugins/charmap/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/directionality': 'npm:@mescius/inputman.richtexteditor/JS/plugins/directionality/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/emoticons': 'npm:@mescius/inputman.richtexteditor/JS/plugins/emoticons/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/fullscreen': 'npm:@mescius/inputman.richtexteditor/JS/plugins/fullscreen/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/htmlcode': 'npm:@mescius/inputman.richtexteditor/JS/plugins/htmlcode/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/image': 'npm:@mescius/inputman.richtexteditor/JS/plugins/image/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/link': 'npm:@mescius/inputman.richtexteditor/JS/plugins/link/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/lists': 'npm:@mescius/inputman.richtexteditor/JS/plugins/lists/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/media': 'npm:@mescius/inputman.richtexteditor/JS/plugins/media/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/pagebreak': 'npm:@mescius/inputman.richtexteditor/JS/plugins/pagebreak/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/preview': 'npm:@mescius/inputman.richtexteditor/JS/plugins/preview/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/save': 'npm:@mescius/inputman.richtexteditor/JS/plugins/save/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/searchreplace': 'npm:@mescius/inputman.richtexteditor/JS/plugins/searchreplace/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/table': 'npm:@mescius/inputman.richtexteditor/JS/plugins/table/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/template': 'npm:@mescius/inputman.richtexteditor/JS/plugins/template/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/wordcount': 'npm:@mescius/inputman.richtexteditor/JS/plugins/wordcount/plugin.js', '@mescius/wijmo.styles': 'npm:@mescius/wijmo.styles', '@mescius/wijmo.cultures': 'npm:@mescius/wijmo.cultures', '@mescius/wijmo.input': 'npm:@mescius/wijmo.input/index.js', '@mescius/wijmo': 'npm:@mescius/wijmo/index.js', '@mescius/wijmo.grid': 'npm:@mescius/wijmo.grid/index.js', '@mescius/wijmo.nav': 'npm:@mescius/wijmo.nav/index.js', '@mescius/wijmo.interop.grid': 'npm:@mescius/wijmo.interop.grid/index.js', '@mescius/wijmo.react.grid': 'npm:@mescius/wijmo.react.grid/index.js', '@mescius/wijmo.react.base': 'npm:@mescius/wijmo.react.base/index.js', '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js', '@mescius/spread-sheets/styles': 'npm:@mescius/spread-sheets/styles', 'react': 'npm:react/umd/react.production.min.js', 'react-dom': 'npm:react-dom/umd/react-dom.production.min.js', 'react-dom/client': 'npm:react-dom/umd/react-dom.production.min.js', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build': 'npm:systemjs-plugin-babel/systemjs-babel-browser.js', }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'jsx', }, 'node_modules': { defaultExtension: 'js', }, }, }); })(this);