-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat: add experimental drag-drop package #11864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package(default_visibility=["//visibility:public"]) | ||
load("@angular//:index.bzl", "ng_module") | ||
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "ts_web_test") | ||
load("@io_bazel_rules_sass//sass:sass.bzl", "sass_binary") | ||
|
||
|
||
ng_module( | ||
name = "drag-drop", | ||
srcs = glob(["**/*.ts"], exclude=["**/*.spec.ts"]), | ||
module_name = "@angular/cdk-experimental/drag-drop", | ||
assets = [":drop.css"], | ||
deps = [ | ||
"@rxjs", | ||
"//src/cdk/platform", | ||
], | ||
tsconfig = "//src/cdk-experimental:tsconfig-build.json", | ||
) | ||
|
||
|
||
ts_library( | ||
name = "drag_and_drop_test_sources", | ||
testonly = 1, | ||
srcs = glob(["**/*.spec.ts"]), | ||
deps = [ | ||
":drag-drop", | ||
"//src/cdk/testing", | ||
], | ||
tsconfig = "//src/cdk-experimental:tsconfig-build.json", | ||
) | ||
|
||
sass_binary( | ||
name = "drop_scss", | ||
src = "drop.scss", | ||
) | ||
|
||
ts_web_test( | ||
name = "unit_tests", | ||
bootstrap = [ | ||
"//:web_test_bootstrap_scripts", | ||
], | ||
tags = ["manual"], | ||
|
||
# Do not sort | ||
deps = [ | ||
"//:tslib_bundle", | ||
"//:angular_bundles", | ||
"//:angular_test_bundles", | ||
"//test:angular_test_init", | ||
":drag_and_drop_test_sources", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {NgModule} from '@angular/core'; | ||
import {CdkDrop} from './drop'; | ||
import {CdkDrag} from './drag'; | ||
import {CdkDragHandle} from './drag-handle'; | ||
import {CdkDragPreview} from './drag-preview'; | ||
import {CdkDragPlaceholder} from './drag-placeholder'; | ||
|
||
@NgModule({ | ||
declarations: [ | ||
CdkDrop, | ||
CdkDrag, | ||
CdkDragHandle, | ||
CdkDragPreview, | ||
CdkDragPlaceholder, | ||
], | ||
exports: [ | ||
CdkDrop, | ||
CdkDrag, | ||
CdkDragHandle, | ||
CdkDragPreview, | ||
CdkDragPlaceholder, | ||
], | ||
}) | ||
export class DragDropModule {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# TODO |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {CdkDrag} from './drag'; | ||
import {CdkDropContainer} from './drop-container'; | ||
|
||
/** Event emitted when the user starts dragging a draggable. */ | ||
export interface CdkDragStart { | ||
/** Draggable that emitted the event. */ | ||
source: CdkDrag; | ||
} | ||
|
||
|
||
/** Event emitted when the user stops dragging a draggable. */ | ||
export interface CdkDragEnd { | ||
/** Draggable that emitted the event. */ | ||
source: CdkDrag; | ||
} | ||
|
||
/** Event emitted when the user moves an item into a new drop container. */ | ||
export interface CdkDragEnter<T> { | ||
/** Container into which the user has moved the item. */ | ||
container: CdkDropContainer<T>; | ||
/** Item that was removed from the container. */ | ||
item: CdkDrag; | ||
} | ||
|
||
/** | ||
* Event emitted when the user removes an item from a | ||
* drop container by moving it into another one. | ||
*/ | ||
export interface CdkDragExit<T> { | ||
/** Container from which the user has a removed an item. */ | ||
container: CdkDropContainer<T>; | ||
/** Item that was removed from the container. */ | ||
item: CdkDrag; | ||
} | ||
|
||
|
||
/** Event emitted when the user drops a draggable item inside a drop container. */ | ||
export interface CdkDragDrop<T, O = T> { | ||
/** Index of the item when it was picked up. */ | ||
previousIndex: number; | ||
/** Current index of the item. */ | ||
currentIndex: number; | ||
/** Item that is being dropped. */ | ||
item: CdkDrag; | ||
/** Container in which the item was dropped. */ | ||
container: CdkDropContainer<T>; | ||
/** Container from which the item was picked up. Can be the same as the `container`. */ | ||
previousContainer: CdkDropContainer<O>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Directive, ElementRef} from '@angular/core'; | ||
|
||
/** Handle that can be used to drag and CdkDrag instance. */ | ||
@Directive({ | ||
selector: '[cdkDragHandle]', | ||
host: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have it on my list, I just haven't added any extra APIs to facilitate it (e.g. we need something pick up and swap items programmatically). |
||
'class': 'cdk-drag-handle' | ||
} | ||
}) | ||
export class CdkDragHandle { | ||
constructor(public element: ElementRef<HTMLElement>) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Directive, TemplateRef, Input} from '@angular/core'; | ||
|
||
/** | ||
* Element that will be used as a template for the placeholder of a CdkDrag when | ||
* it is being dragged. The placeholder is displayed in place of the element being dragged. | ||
*/ | ||
@Directive({ | ||
selector: 'ng-template[cdkDragPlaceholder]' | ||
}) | ||
export class CdkDragPlaceholder<T = any> { | ||
/** Context data to be added to the placeholder template instance. */ | ||
@Input() data: T; | ||
constructor(public templateRef: TemplateRef<T>) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Directive, TemplateRef, Input} from '@angular/core'; | ||
|
||
/** | ||
* Element that will be used as a template for the preview | ||
* of a CdkDrag when it is being dragged. | ||
*/ | ||
@Directive({ | ||
selector: 'ng-template[cdkDragPreview]' | ||
}) | ||
export class CdkDragPreview<T = any> { | ||
/** Context data to be added to the preview template instance. */ | ||
@Input() data: T; | ||
constructor(public templateRef: TemplateRef<T>) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/** | ||
* Moves an item one index in an array to another. | ||
* @param array Array in which to move the item. | ||
* @param fromIndex Starting index of the item. | ||
* @param toIndex Index to which the item should be moved. | ||
*/ | ||
export function moveItemInArray<T = any>(array: T[], fromIndex: number, toIndex: number): void { | ||
if (fromIndex === toIndex) { | ||
return; | ||
} | ||
|
||
const target = array[fromIndex]; | ||
const delta = toIndex < fromIndex ? -1 : 1; | ||
|
||
for (let i = fromIndex; i !== toIndex; i += delta) { | ||
array[i] = array[i + delta]; | ||
} | ||
|
||
array[toIndex] = target; | ||
} | ||
|
||
|
||
/** | ||
* Moves an item from one array to another. | ||
* @param currentArray Array from which to transfer the item. | ||
* @param targetArray Array into which to put the item. | ||
* @param currentIndex Index of the item in its current array. | ||
* @param targetIndex Index at which to insert the item. | ||
*/ | ||
export function transferArrayItem<T = any>(currentArray: T[], | ||
targetArray: T[], | ||
currentIndex: number, | ||
targetIndex: number): void { | ||
targetArray.splice(targetIndex, 0, currentArray.splice(currentIndex, 1)[0]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do dragged items have to inherently have an index?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The drop event only fires on items inside a drop zone which ensures that they have an index.