gpt4 book ai didi

javascript - DraftJs:使用实体键替换实体

转载 作者:行者123 更新时间:2023-12-03 06:59:41 32 4
gpt4 key购买 nike

我正在使用 draftjs 创建富文本编辑器并且找不到任何资源来帮助我解决问题。
请先查看codesandbox .
您可以看到包含链接的文本(testtest 为红色)。如果单击它,您将在表格中看到链接的一些信息:

| 🔗 link src         | http://localhost:8080/testtest |
| 📝 link text | testtest |
| 🔑 link Entity key | ab5a7c6d... |
感谢我的 getCurrentLinkKey,我得到了当前的链接 key (🔑) helper :
const getCurrentLinkKey = (
editorState: EditorState,
contentState?: ContentState
): string => {
if (contentState === undefined) {
contentState = editorState.getCurrentContent();
}

const startKey = editorState.getSelection().getStartKey();
const startOffset = editorState.getSelection().getStartOffset();
const blockWithLinkAtBeginning = contentState.getBlockForKey(startKey);

return blockWithLinkAtBeginning.getEntityAt(startOffset);
};
然后用这个键我可以得到链接 Entity使用 getCurrentLinkEntity helper :
const getCurrentLinkEntity = (
editorState: EditorState
): EntityInstance | null => {
const contentState = editorState.getCurrentContent();
const linkKey = getCurrentLinkKey(editorState, contentState);

if (linkKey) {
return contentState.getEntity(linkKey);
}

return null;
};
使用链接 Entity我终于可以得到 srctext值(value):
getCurrentLinkEntity(editorState).getData().url   // 🔗
getCurrentLinkEntity(editorState).getData().text // 📝

您可以在底部看到一个按钮 Insert link .如果您选择整个链接 testtest并单击此按钮,链接将被替换。
此功能由 insertLink 处理。 helper :
const insertLink = (
link: string,
text: string,
editorState: EditorState,
setEditorState: (editorState: EditorState) => void
): void => {
const contentStateWithEntity = editorState
.getCurrentContent()
.createEntity("LINK", "MUTABLE", { url: link, text });

const entityKey = contentStateWithEntity.getLastCreatedEntityKey();

const contentState = Modifier.replaceText(
editorState.getCurrentContent(),
editorState.getSelection(),
text,
editorState.getCurrentInlineStyle(),
entityKey
);

const newEditorState = EditorState.set(editorState, {
currentContent: contentStateWithEntity
});
const newEditorStateWithLink = RichUtils.toggleLink(
newEditorState,
newEditorState.getSelection(),
entityKey
);

setEditorState(
EditorState.push(newEditorStateWithLink, contentState, "insert-characters")
);
};
但是这个功能只会用谷歌链接替换你选择的文本。我想要的是,如果您在链接上并单击按钮,那么应该更新整个链接。所以我创建了 replaceLink helper :
const replaceLink = (
link: string,
text: string,
editorState: EditorState,
setEditorState: (editorState: EditorState) => void
): void => {
const contentStateWithEntity = editorState
.getCurrentContent()
.createEntity("LINK", "MUTABLE", { url: link, text });

const entityKey = contentStateWithEntity.getLastCreatedEntityKey();

const newEditorState = EditorState.set(editorState, {
currentContent: contentStateWithEntity
});

const contentState = newEditorState
.getCurrentContent()
.replaceEntityData(getCurrentLinkKey(editorState), { entityKey });

const newEditorStateWithLink = RichUtils.toggleLink(
newEditorState,
newEditorState.getSelection(),
entityKey
);

setEditorState(
EditorState.push(newEditorStateWithLink, contentState, "insert-characters")
);
};
但可悲的是,如果我点击 Replace link按钮(触发 replaceLink 助手),链接未更新,但 srctext是空的:
| 🔗 link src         |             |
| 📝 link text | |
| 🔑 link Entity key | a1e34047... |
所以有人知道我可以如何替换链接 Entity使用它的实体键?

最佳答案

好吧,我给出了一个使用 draftjs-utils 的答案打包并替换链接但不使用其实体键 :
这个replaceLink helper 的灵感来自 react-draft-wysiwyg图书馆:

export const replaceLink = (
link: string,
text: string,
editorState: EditorState,
setEditorState: (editorState: EditorState) => void
): void => {
const currentContent = editorState.getCurrentContent();

// Create new link entity
const contentStateWithEntity = currentContent.createEntity(
"LINK",
"MUTABLE",
{ url: link, text }
);

// Get created link entity's key
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();

let selection = editorState.getSelection();

const entityRange = getEntityRange(
editorState,
getSelectionEntity(editorState)
);

const isBackward = selection.getIsBackward();

if (isBackward) {
selection = selection.merge({
anchorOffset: entityRange.end,
focusOffset: entityRange.start
});
} else {
selection = selection.merge({
anchorOffset: entityRange.start,
focusOffset: entityRange.end
});
}

const updatedEditorWithText = Modifier.replaceText(
currentContent,
selection,
text,
editorState.getCurrentInlineStyle(),
entityKey
);

const newEditorState = EditorState.push(
editorState,
updatedEditorWithText,
"insert-characters"
);

setEditorState(
EditorState.push(newEditorState, updatedEditorWithText, "insert-characters")
);
};
查看 codesandbox现场观看。

关于javascript - DraftJs:使用实体键替换实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63489668/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com