Skip to content

Commit

Permalink
feat(blocks): Add onTextSelection event to Html.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannMoller committed Oct 5, 2023
1 parent ab633e6 commit 4f00d5e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/plugins/blocks/blocks-basic/src/blocks/Html/Html.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import React from 'react';
import { blockDefaultProps, HtmlComponent } from '@lowdefy/block-utils';

const HtmlBlock = ({ blockId, properties, methods }) => (
const HtmlBlock = ({ blockId, events, properties, methods }) => (
<HtmlComponent
div={true}
events={events}
html={properties.html}
id={blockId}
methods={methods}
Expand Down
15 changes: 15 additions & 0 deletions packages/utils/block-utils/src/HtmlComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class HtmlComponent extends React.Component {
this.div = {
innerHTML: '',
};
this.onTextSelection = this.onTextSelection.bind(this);
}

componentDidMount() {
Expand All @@ -36,6 +37,19 @@ class HtmlComponent extends React.Component {
this.div.innerHTML = DOMPurify.sanitize(htmlString);
}

onTextSelection() {
if (this.props.events.onTextSelection) {
const selection = window.getSelection().toString();

Check warning on line 42 in packages/utils/block-utils/src/HtmlComponent.js

View check run for this annotation

Codecov / codecov/patch

packages/utils/block-utils/src/HtmlComponent.js#L42

Added line #L42 was not covered by tests
if (selection !== '') {
this.props.methods.triggerEvent({

Check warning on line 44 in packages/utils/block-utils/src/HtmlComponent.js

View check run for this annotation

Codecov / codecov/patch

packages/utils/block-utils/src/HtmlComponent.js#L44

Added line #L44 was not covered by tests
name: 'onTextSelection',
event: {
selection: selection,
},
});
}
}
}
render() {
const { div, id, methods, style } = this.props;
if (div === true) {
Expand All @@ -49,6 +63,7 @@ class HtmlComponent extends React.Component {
}
}}
className={methods.makeCssClass(style)}
onMouseUp={this.onTextSelection}
/>
);
}
Expand Down

0 comments on commit 4f00d5e

Please sign in to comment.