Skip to content

Commit

Permalink
feat(*): add drag and drop for images
Browse files Browse the repository at this point in the history
Signed-off-by: Diana Lease <[email protected]>
  • Loading branch information
DianaLease committed Jun 18, 2020
1 parent da8415d commit 3b18848
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
11 changes: 8 additions & 3 deletions packages/ui-contract-editor/src/lib/ContractEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,19 @@ const ContractEditor = (props) => {
};

const onDragOver = (editor, event) => {
event.preventDefault();
event.stopPropagation();
event.dataTransfer.dropEffect = 'move';
};

// return true if should continue through to next handler, else return false
const onDrop = (editor, event) => {
event.preventDefault();
const targetRange = ReactEditor.findEventRange(editor, event);
const [targetIsClause] = Editor.nodes(editor, { match: n => n.type === 'clause', at: targetRange });
if (targetIsClause) return false; // do not allow dropping inside of a clause
const sourceRange = JSON.parse(event.dataTransfer.getData('text'));
const [clauseNode] = Editor.nodes(editor, { match: n => n.type === 'clause', at: sourceRange });
if (!clauseNode) return; // only allow dropping of clause nodes
const targetRange = ReactEditor.findEventRange(editor, event);
if (!clauseNode) return true; // continue to next handler if not a clause
const node = ReactEditor.toSlateNode(editor, event.target);
const path = ReactEditor.findPath(editor, node);

Expand Down Expand Up @@ -180,6 +183,7 @@ const ContractEditor = (props) => {
Transforms.collapse(editor, { edge });
Transforms.removeNodes(editor, { at: sourceRange.anchor.path, match: n => n.type === 'clause' });
Transforms.insertNodes(editor, clauseNode[0]);
return false;
};

return (
Expand All @@ -203,6 +207,7 @@ const ContractEditor = (props) => {
);
};


/**
* The property types for this component
*/
Expand Down
34 changes: 31 additions & 3 deletions packages/ui-markdown-editor/src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const MarkdownEditor = (props) => {
const renderElement = useCallback((slateProps) => {
const elementProps = { ...slateProps, customElements: props.customElements, editor };
return (<Element {...elementProps} />);
}, [props.customElements]);
}, [props.customElements, editor]);

const hotkeyActions = {
mark: code => toggleMark(editor, code),
Expand Down Expand Up @@ -137,6 +137,34 @@ export const MarkdownEditor = (props) => {
}
};

const handleDragStart = (event) => {
event.stopPropagation();
if (props.onDragStart) {
props.onDragStart(editor, event);
}
const node = ReactEditor.toSlateNode(editor, event.target);
const path = ReactEditor.findPath(editor, node);
const range = Editor.range(editor, path);
event.dataTransfer.setData('text', JSON.stringify(range));
};

const handleDrop = (event) => {
event.preventDefault();
if (props.onDrop) {
const shouldContinue = props.onDrop(editor, event);
if (!shouldContinue) return;
}
const sourceRange = JSON.parse(event.dataTransfer.getData('text'));
const [imageNode] = Editor.nodes(editor, { match: n => n.type === 'image', at: sourceRange });
const targetRange = ReactEditor.findEventRange(editor, event);
if (imageNode) {
Transforms.select(editor, targetRange);
Transforms.collapse(editor);
Transforms.removeNodes(editor, { at: sourceRange, match: n => n.type === 'image' });
Transforms.insertNodes(editor, imageNode[0]);
}
};

return (
<Slate editor={editor} value={props.value} onChange={onChange}>
{ !props.readOnly
Expand All @@ -159,9 +187,9 @@ export const MarkdownEditor = (props) => {
onDOMBeforeInput={onBeforeInput}
onCopy={handleCopyOrCut}
onCut={event => handleCopyOrCut(event, true)}
onDragStart={event => props.onDragStart ? props.onDragStart(editor, event) : null}
onDragStart={handleDragStart}
onDragOver={event => props.onDragOver ? props.onDragOver(editor, event) : null}
onDrop={event => props.onDrop ? props.onDrop(editor, event) : null}
onDrop={handleDrop}
/>
</Slate>
);
Expand Down
1 change: 0 additions & 1 deletion packages/ui-markdown-editor/src/lib/plugins/withImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { POPUP_STYLE } from '../utilities/constants';
import Button from '../components/Button';

const StyledImage = styled.img`
display: block;
max-width: 100%;
max-height: 20em;
box-shadow: ${props => (props.shadow ? '0 0 0 3px #B4D5FF' : 'none')};
Expand Down

0 comments on commit 3b18848

Please sign in to comment.