-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nodeview with content story (#1865)
Co-authored-by: Idriss mahjoubi <mahjoubi.idriss@gmail.com>
- Loading branch information
1 parent
2c96b54
commit 745ef53
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
packages/storybook-react/stories/react-components/node-with-content.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import './style/card.css'; | ||
|
||
import React, { ComponentType } from 'react'; | ||
import { | ||
DOMCompatibleAttributes, | ||
ExtensionTag, | ||
NodeExtension, | ||
NodeExtensionSpec, | ||
} from '@remirror/core'; | ||
import { EditorComponent, NodeViewComponentProps, Remirror, useRemirror } from '@remirror/react'; | ||
|
||
const userAttrs = { | ||
id: 'randomId', | ||
name: 'John Doe', | ||
src: 'https://dummyimage.com/2000x800/479e0c/fafafa', | ||
}; | ||
const UserCardContent = `<div data-user-id="${userAttrs.id}" data-user-name="${userAttrs.name}" data-user-image-url="${userAttrs.src}"><p>This is editable content...</p></div>`; | ||
|
||
export default { title: 'Components (labs) / Card with content' }; | ||
|
||
class UserCardExtension extends NodeExtension { | ||
get name() { | ||
return 'user-card' as const; | ||
} | ||
|
||
ReactComponent: ComponentType<NodeViewComponentProps> = ({ node, forwardRef }) => { | ||
const { name, imageSrc } = node.attrs; | ||
|
||
return ( | ||
<div className='card'> | ||
<div contentEditable='false'> | ||
<img src={imageSrc} alt='Avatar' style={{ width: '100%' }} /> | ||
<h4> | ||
<b>{name}</b> | ||
</h4> | ||
<span>Write user description below</span> | ||
</div> | ||
<p ref={forwardRef} /> | ||
</div> | ||
); | ||
}; | ||
|
||
createTags() { | ||
return [ExtensionTag.Block]; | ||
} | ||
createNodeSpec(): NodeExtensionSpec { | ||
return { | ||
attrs: { | ||
id: { default: null }, | ||
name: { default: '' }, | ||
imageSrc: { default: '' }, | ||
}, | ||
content: 'block*', | ||
toDOM: (node) => { | ||
const attrs: DOMCompatibleAttributes = { | ||
'data-user-id': node.attrs.id, | ||
'data-user-name': node.attrs.name, | ||
'data-user-image-url': node.attrs.imageSrc, | ||
}; | ||
return ['div', attrs, 0]; | ||
}, | ||
parseDOM: [ | ||
{ | ||
attrs: { | ||
id: { default: null }, | ||
name: { default: '' }, | ||
imageSrc: { default: '' }, | ||
}, | ||
tag: 'div[data-user-id]', | ||
getAttrs: (dom) => { | ||
const node = dom as HTMLAnchorElement; | ||
const id = node.getAttribute('data-user-id'); | ||
const name = node.getAttribute('data-user-name'); | ||
const imageSrc = node.getAttribute('data-user-image-url'); | ||
|
||
return { | ||
id, | ||
name, | ||
imageSrc, | ||
}; | ||
}, | ||
}, | ||
], | ||
}; | ||
} | ||
} | ||
|
||
const extensions = () => [new UserCardExtension({ disableExtraAttributes: true })]; | ||
|
||
export const UserCard = () => { | ||
const { manager, state } = useRemirror({ | ||
extensions, | ||
content: UserCardContent, | ||
stringHandler: 'html', | ||
}); | ||
|
||
return ( | ||
<Remirror manager={manager} initialContent={state} autoFocus> | ||
<EditorComponent /> | ||
</Remirror> | ||
); | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/storybook-react/stories/react-components/style/card.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.card { | ||
border: 1px solid #eee; | ||
box-shadow: 0 2px 2px #ccc; | ||
width: 200px; | ||
padding: 20px; | ||
margin: 20px; | ||
} |
745ef53
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://remirror.io as production
🚀 Deployed on https://6328976b6baf4e102ca898c4--remirror.netlify.app