Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit 3b18848

Browse files
committed
feat(*): add drag and drop for images
Signed-off-by: Diana Lease <[email protected]>
1 parent da8415d commit 3b18848

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

packages/ui-contract-editor/src/lib/ContractEditor/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,19 @@ const ContractEditor = (props) => {
138138
};
139139

140140
const onDragOver = (editor, event) => {
141-
event.preventDefault();
141+
event.stopPropagation();
142142
event.dataTransfer.dropEffect = 'move';
143143
};
144144

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

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

185189
return (
@@ -203,6 +207,7 @@ const ContractEditor = (props) => {
203207
);
204208
};
205209

210+
206211
/**
207212
* The property types for this component
208213
*/

packages/ui-markdown-editor/src/lib/index.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const MarkdownEditor = (props) => {
5353
const renderElement = useCallback((slateProps) => {
5454
const elementProps = { ...slateProps, customElements: props.customElements, editor };
5555
return (<Element {...elementProps} />);
56-
}, [props.customElements]);
56+
}, [props.customElements, editor]);
5757

5858
const hotkeyActions = {
5959
mark: code => toggleMark(editor, code),
@@ -137,6 +137,34 @@ export const MarkdownEditor = (props) => {
137137
}
138138
};
139139

140+
const handleDragStart = (event) => {
141+
event.stopPropagation();
142+
if (props.onDragStart) {
143+
props.onDragStart(editor, event);
144+
}
145+
const node = ReactEditor.toSlateNode(editor, event.target);
146+
const path = ReactEditor.findPath(editor, node);
147+
const range = Editor.range(editor, path);
148+
event.dataTransfer.setData('text', JSON.stringify(range));
149+
};
150+
151+
const handleDrop = (event) => {
152+
event.preventDefault();
153+
if (props.onDrop) {
154+
const shouldContinue = props.onDrop(editor, event);
155+
if (!shouldContinue) return;
156+
}
157+
const sourceRange = JSON.parse(event.dataTransfer.getData('text'));
158+
const [imageNode] = Editor.nodes(editor, { match: n => n.type === 'image', at: sourceRange });
159+
const targetRange = ReactEditor.findEventRange(editor, event);
160+
if (imageNode) {
161+
Transforms.select(editor, targetRange);
162+
Transforms.collapse(editor);
163+
Transforms.removeNodes(editor, { at: sourceRange, match: n => n.type === 'image' });
164+
Transforms.insertNodes(editor, imageNode[0]);
165+
}
166+
};
167+
140168
return (
141169
<Slate editor={editor} value={props.value} onChange={onChange}>
142170
{ !props.readOnly
@@ -159,9 +187,9 @@ export const MarkdownEditor = (props) => {
159187
onDOMBeforeInput={onBeforeInput}
160188
onCopy={handleCopyOrCut}
161189
onCut={event => handleCopyOrCut(event, true)}
162-
onDragStart={event => props.onDragStart ? props.onDragStart(editor, event) : null}
190+
onDragStart={handleDragStart}
163191
onDragOver={event => props.onDragOver ? props.onDragOver(editor, event) : null}
164-
onDrop={event => props.onDrop ? props.onDrop(editor, event) : null}
192+
onDrop={handleDrop}
165193
/>
166194
</Slate>
167195
);

packages/ui-markdown-editor/src/lib/plugins/withImages.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { POPUP_STYLE } from '../utilities/constants';
1515
import Button from '../components/Button';
1616

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

0 commit comments

Comments
 (0)