fix(drag-drop): not working correctly inside transplanted views #18499
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently the CDK drag&drop is set up so that it keeps track of the items inside each drop list via
ContentChildren
, however this doesn't work properly inside of a transplanted view (e.g. in the header of amat-table
). There are two main problems inside transplanted views:ContentChildren
query may be empty, even though the items exist. Usually they're fine on the second run, but that may be too late, because the user could've started dragging already.ContentChildren
is up-to-date when dragging starts, Angular won't update the order of the transplanted view items in the query once they're shuffled around.To work around these limitations and to make it possible to support more advanced usages like in
mat-table
, these changes switch to keeping track of the items using DI. Whenever an item is created or destroyed, it registers/deregisters itself with the closest drop list. Since the insertion order can be different from the DOM order, we usecompareDocumentPosition
to sort the items right before dragging starts.Fixes #18482.