Skip to content

Commit

Permalink
Add nodeview with content story (#1865)
Browse files Browse the repository at this point in the history
Co-authored-by: Idriss mahjoubi <mahjoubi.idriss@gmail.com>
  • Loading branch information
IdrissMahjoubi and Idriss mahjoubi committed Sep 19, 2022
1 parent 2c96b54 commit 745ef53
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
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>
);
};
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;
}

1 comment on commit 745ef53

@github-actions
Copy link
Contributor

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

Please sign in to comment.