Skip to content

Commit

Permalink
Clean up correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 2, 2024
1 parent 5ce6f4e commit 8e8f6d5
Showing 1 changed file with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@
*/
import { useRefEffect } from '@wordpress/compose';

class NodeSet extends Set {
constructor() {
super();
this.documents = new Set();
}
add( node ) {
const { ownerDocument } = node;
if ( ! this.documents.has( ownerDocument ) ) {
ownerDocument.addEventListener( 'pointerdown', down );
this.documents.add( ownerDocument );
}
return super.add( node );
const nodesByDocument = new Map();

function add( doc, node ) {
let set = nodesByDocument.get( doc );
if ( ! set ) {
set = new Set();
nodesByDocument.set( doc, set );
doc.addEventListener( 'pointerdown', down );
}
delete( node ) {
set.add( node );
}

function remove( doc, node ) {
const set = nodesByDocument.get( doc );
if ( set ) {
set.delete( node );
restore( node );
return super.delete( node );
if ( set.size === 0 ) {
nodesByDocument.delete( doc );
doc.removeEventListener( 'pointerdown', down );
}
}
}

const nodes = new NodeSet();

function restore( node ) {
const prevDraggable = node.getAttribute( 'data-draggable' );
if ( prevDraggable ) {
Expand All @@ -37,13 +40,17 @@ function restore( node ) {
}

function down( event ) {
if ( event.target.isContentEditable ) {
const { target } = event;
const { ownerDocument, isContentEditable } = target;
const nodes = nodesByDocument.get( ownerDocument );

if ( isContentEditable ) {
// Whenever an editable element is clicked, check which draggable
// blocks contain this element, and temporarily disable draggability.
for ( const node of nodes ) {
if (
node.getAttribute( 'draggable' ) === 'true' &&
node.contains( event.target )
node.contains( target )
) {
node.removeAttribute( 'draggable' );
node.setAttribute( 'data-draggable', 'true' );
Expand All @@ -68,9 +75,9 @@ function down( event ) {
*/
export function useFirefoxDraggableCompatibility() {
return useRefEffect( ( node ) => {
nodes.add( node );
add( node.ownerDocument, node );
return () => {
nodes.delete( node );
remove( node.ownerDocument, node );
};
}, [] );
}

0 comments on commit 8e8f6d5

Please sign in to comment.