Skip to content

Commit

Permalink
add support for <Label/> within slate
Browse files Browse the repository at this point in the history
  • Loading branch information
nileshgulia1 committed Mar 31, 2022
1 parent 482b91a commit dfa7ac6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 39 deletions.
30 changes: 30 additions & 0 deletions src/editor/LabelWrapper.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { Popup, Label } from 'semantic-ui-react';
import { serializeNodes } from 'volto-slate/editor/render';

const LabelWrapper = (props) => {
const { attributes, children, element } = props;
const { data = {} } = element;
const { uid, label_type } = data;
return (
<Popup
position={data.pointing}
open={data.always_show || undefined}
on={!data.always_show && 'click'}
trigger={
label_type ? (
<Label className={label_type}>{children}</Label>
) : (
<span id={`label_ref-${uid}`} {...attributes} className="label-node">
{children}
</span>
)
}
className={`ui label ${data.type}`}
>
{serializeNodes(data.label_content)}
</Popup>
);
};

export default LabelWrapper;
41 changes: 4 additions & 37 deletions src/editor/render.jsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,19 @@
import React from 'react';
import { Popup } from 'semantic-ui-react';
import { serializeNodes } from 'volto-slate/editor/render';
import LabelWrapper from './LabelWrapper';

export const LabelElement = (props) => {
const { attributes, children, element, mode } = props;
const { element, mode } = props;
const { data = {} } = element;
const { uid } = data;

return (
<>
{mode === 'view' ? (
<span id={`ref-${uid}`} aria-describedby="slate-label">
<Popup
position={data.pointing}
open={data.always_show || undefined}
on={!data.always_show && 'click'}
trigger={
<span
id={`cite_ref-${uid}`}
{...attributes}
className="label-node"
>
{children}
</span>
}
className={`ui label ${data.type}`}
>
{serializeNodes(data.label_content)}
</Popup>
<LabelWrapper {...props} />
</span>
) : (
<Popup
position={data.pointing}
open={data.always_show || undefined}
on={!data.always_show && 'click'}
trigger={
<span
id={`label_ref-${uid}`}
{...attributes}
className="label-node"
>
{children}
</span>
}
className={`ui label ${data.type}`}
>
{serializeNodes(data.label_content)}
</Popup>
<LabelWrapper {...props} />
)}
</>
);
Expand Down
19 changes: 17 additions & 2 deletions src/editor/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ export const LabelEditorSchema = {
{
id: 'default',
title: 'Default',
fields: ['label_type'],
},
{
id: 'tooltip',
title: 'Tooltip',
fields: ['label_content', 'pointing', 'type', 'always_show'],
},
],
properties: {
label_content: {
title: 'Content',
widget: 'slate_richtext',
widget: 'slate',
},
pointing: {
title: 'Pointing',
Expand Down Expand Up @@ -38,9 +43,19 @@ export const LabelEditorSchema = {
],
default: ['highlight', 'highlight'],
},
label_type: {
title: 'Type',
type: 'string',
factory: 'Choice',
choices: [
['medium', 'medium'],
['high', 'high'],
['highlight', 'highlight'],
],
},
always_show: {
title: 'Always show',
description: 'Always show the content label popup',
description: 'Always show the content label tooltip',
type: 'boolean',
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { LABEL } from './constants';
import installLabelEditor from './editor';
import RichTextWidgetView from 'volto-slate/widgets/RichTextWidgetView';

export default function install(config) {
config.settings.labels = [...(config.settings.labels || []), LABEL];
config.widgets.views.widget.slate = RichTextWidgetView;
config = installLabelEditor(config);

return config;
Expand Down

0 comments on commit dfa7ac6

Please sign in to comment.