我正在尝试创建一个按钮,onClick 将为您提供放置链接的选项,然后该链接必须在文本字段中显示为标签。到目前为止,我设法添加并显示纯文本,但我不知道如何将其转换为标签。
// Link function
const addLinkFn = (editorState, link) => {
const contentState = editorState.getCurrentContent();
const contentStateWithEntity = contentState.createEntity(
"LINK",
"IMMUTABLE",
{ url: link }
);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
const newEditorState = EditorState.set(editorState, {
currentContent: contentStateWithEntity,
});
return AtomicBlockUtils.insertAtomicBlock(newEditorState, entityKey, link);
};
const onAddLink = () => {
let link = window.prompt("Add link http:// ");
const newEditorState = addLinkFn(editorState, link);
setEditorState(newEditorState);
};
Run Code Online (Sandbox Code Playgroud)
我知道在某个地方我必须有一个像这样的“装饰器”和“策略”:
const linkStrategy = (contentBlock, callback, contentState) => {
contentBlock.findEntityRanges((character) => {
const entityKey = character.getEntity(); …Run Code Online (Sandbox Code Playgroud) 我正在创建 RTS 游戏,其中一项功能是构建不同类型的建筑物。我发现了很多重复,我想在辅助方法中提取它,但问题是每个建筑物都是不同的对象,它们从主建筑物类中吸收了一些属性。
构建方法如下所示:
public static void buildDockyard(Base base) {
if (Validator.checkForBuilding(base, "Dockyard")) {
throw new IllegalStateException("Dockyard is already build");
}
Dockyard dockyard = new Dockyard("Dockyard");
int requiredPower = dockyard.requiredResource("power");
int requiredStardust = dockyard.requiredResource("stardust");
int requiredPopulation = dockyard.requiredResource("population");
Validator.checkResource(base, requiredPower, requiredStardust, requiredPopulation);
updateResourceAfterBuild(base, requiredPower, requiredStardust, requiredPopulation);
dockyard.setCompleteTime(dockyard.requiredResource("time"));
base.getBuildings().add(dockyard);
}
public static void buildHotel(Base base) {
if (Validator.checkForBuilding(base, "Space Hotel")) {
throw new IllegalStateException("Space Hotel is already build");
}
SpaceHotel spaceHotel = new SpaceHotel("Space Hotel");
int requiredPower = spaceHotel.requiredResource("power");
int requiredStardust …Run Code Online (Sandbox Code Playgroud)