Skip to content

refactor: change gutter directives to modern angular #479

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

Merged
merged 3 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { Directive, OnInit, OnDestroy, Inject, ElementRef } from '@angular/core'
import { Directive, OnDestroy, ElementRef, inject } from '@angular/core'
import { SplitGutterDirective } from './split-gutter.directive'
import { GUTTER_NUM_TOKEN } from './gutter-num-token'

@Directive({
selector: '[asSplitGutterDragHandle]',
standalone: true,
})
export class SplitGutterDragHandleDirective implements OnInit, OnDestroy {
constructor(
@Inject(GUTTER_NUM_TOKEN) private gutterNum: number,
private elementRef: ElementRef<HTMLElement>,
private gutterDir: SplitGutterDirective,
) {}
export class SplitGutterDragHandleDirective implements OnDestroy {
private readonly gutterNum = inject(GUTTER_NUM_TOKEN)
private readonly elementRef = inject<ElementRef<HTMLElement>>(ElementRef)
private readonly gutterDir = inject(SplitGutterDirective)

ngOnInit(): void {
this.gutterDir.addToMap(this.gutterDir.gutterToHandleElementMap, this.gutterNum, this.elementRef)
constructor() {
this.gutterDir._addToMap(this.gutterDir._gutterToHandleElementMap, this.gutterNum, this.elementRef)
}

ngOnDestroy(): void {
this.gutterDir.removedFromMap(this.gutterDir.gutterToHandleElementMap, this.gutterNum, this.elementRef)
this.gutterDir._removedFromMap(this.gutterDir._gutterToHandleElementMap, this.gutterNum, this.elementRef)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injector, Directive, Input, ViewContainerRef, TemplateRef } from '@angular/core'
import { Injector, Directive, ViewContainerRef, TemplateRef, input, effect, inject } from '@angular/core'
import { GUTTER_NUM_TOKEN } from './gutter-num-token'

interface SplitGutterDynamicInjectorTemplateContext {
Expand All @@ -14,30 +14,31 @@ interface SplitGutterDynamicInjectorTemplateContext {
standalone: true,
})
export class SplitGutterDynamicInjectorDirective {
@Input('asSplitGutterDynamicInjector')
set gutterNum(value: number) {
this.vcr.clear()
private readonly vcr = inject(ViewContainerRef)
private readonly templateRef = inject<TemplateRef<SplitGutterDynamicInjectorTemplateContext>>(TemplateRef)

const injector = Injector.create({
providers: [
{
provide: GUTTER_NUM_TOKEN,
useValue: value,
},
],
parent: this.vcr.injector,
})
protected readonly gutterNum = input.required<number>({ alias: 'asSplitGutterDynamicInjector' })

this.vcr.createEmbeddedView(this.templateRef, { $implicit: injector })
}
constructor() {
effect(() => {
this.vcr.clear()

const injector = Injector.create({
providers: [
{
provide: GUTTER_NUM_TOKEN,
useValue: this.gutterNum(),
},
],
parent: this.vcr.injector,
})

constructor(
private vcr: ViewContainerRef,
private templateRef: TemplateRef<SplitGutterDynamicInjectorTemplateContext>,
) {}
this.vcr.createEmbeddedView(this.templateRef, { $implicit: injector })
})
}

static ngTemplateContextGuard(
dir: SplitGutterDynamicInjectorDirective,
_dir: SplitGutterDynamicInjectorDirective,
ctx: unknown,
): ctx is SplitGutterDynamicInjectorTemplateContext {
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { Directive, OnInit, OnDestroy, Inject, ElementRef } from '@angular/core'
import { Directive, OnDestroy, ElementRef, inject } from '@angular/core'
import { SplitGutterDirective } from './split-gutter.directive'
import { GUTTER_NUM_TOKEN } from './gutter-num-token'

@Directive({
selector: '[asSplitGutterExcludeFromDrag]',
standalone: true,
})
export class SplitGutterExcludeFromDragDirective implements OnInit, OnDestroy {
constructor(
@Inject(GUTTER_NUM_TOKEN) private gutterNum: number,
private elementRef: ElementRef<HTMLElement>,
private gutterDir: SplitGutterDirective,
) {}
export class SplitGutterExcludeFromDragDirective implements OnDestroy {
private readonly gutterNum = inject(GUTTER_NUM_TOKEN)
private readonly elementRef = inject<ElementRef<HTMLElement>>(ElementRef)
private readonly gutterDir = inject(SplitGutterDirective)

ngOnInit(): void {
this.gutterDir.addToMap(this.gutterDir.gutterToExcludeDragElementMap, this.gutterNum, this.elementRef)
constructor() {
this.gutterDir._addToMap(this.gutterDir._gutterToExcludeDragElementMap, this.gutterNum, this.elementRef)
}

ngOnDestroy(): void {
this.gutterDir.removedFromMap(this.gutterDir.gutterToExcludeDragElementMap, this.gutterNum, this.elementRef)
this.gutterDir._removedFromMap(this.gutterDir._gutterToExcludeDragElementMap, this.gutterNum, this.elementRef)
}
}
39 changes: 26 additions & 13 deletions projects/angular-split/src/lib/gutter/split-gutter.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, ElementRef, TemplateRef } from '@angular/core'
import { Directive, ElementRef, inject, TemplateRef } from '@angular/core'
import { SplitAreaComponent } from '../split-area/split-area.component'

export interface SplitGutterTemplateContext {
Expand Down Expand Up @@ -38,22 +38,29 @@ export interface SplitGutterTemplateContext {
standalone: true,
})
export class SplitGutterDirective {
readonly template = inject<TemplateRef<SplitGutterTemplateContext>>(TemplateRef)

/**
* The map holds reference to the drag handle elements inside instances
* of the provided template.
*
* @internal
*/
gutterToHandleElementMap = new Map<number, ElementRef<HTMLElement>[]>()
readonly _gutterToHandleElementMap = new Map<number, ElementRef<HTMLElement>[]>()
/**
* The map holds reference to the excluded drag elements inside instances
* of the provided template.
*
* @internal
*/
gutterToExcludeDragElementMap = new Map<number, ElementRef<HTMLElement>[]>()

constructor(public template: TemplateRef<SplitGutterTemplateContext>) {}
readonly _gutterToExcludeDragElementMap = new Map<number, ElementRef<HTMLElement>[]>()

canStartDragging(originElement: HTMLElement, gutterNum: number) {
if (this.gutterToExcludeDragElementMap.has(gutterNum)) {
const isInsideExclude = this.gutterToExcludeDragElementMap
/**
* @internal
*/
_canStartDragging(originElement: HTMLElement, gutterNum: number) {
if (this._gutterToExcludeDragElementMap.has(gutterNum)) {
const isInsideExclude = this._gutterToExcludeDragElementMap
.get(gutterNum)
.some((gutterExcludeElement) => gutterExcludeElement.nativeElement.contains(originElement))

Expand All @@ -62,24 +69,30 @@ export class SplitGutterDirective {
}
}

if (this.gutterToHandleElementMap.has(gutterNum)) {
return this.gutterToHandleElementMap
if (this._gutterToHandleElementMap.has(gutterNum)) {
return this._gutterToHandleElementMap
.get(gutterNum)
.some((gutterHandleElement) => gutterHandleElement.nativeElement.contains(originElement))
}

return true
}

addToMap(map: Map<number, ElementRef<HTMLElement>[]>, gutterNum: number, elementRef: ElementRef<HTMLElement>) {
/**
* @internal
*/
_addToMap(map: Map<number, ElementRef<HTMLElement>[]>, gutterNum: number, elementRef: ElementRef<HTMLElement>) {
if (map.has(gutterNum)) {
map.get(gutterNum).push(elementRef)
} else {
map.set(gutterNum, [elementRef])
}
}

removedFromMap(map: Map<number, ElementRef<HTMLElement>[]>, gutterNum: number, elementRef: ElementRef<HTMLElement>) {
/**
* @internal
*/
_removedFromMap(map: Map<number, ElementRef<HTMLElement>[]>, gutterNum: number, elementRef: ElementRef<HTMLElement>) {
const elements = map.get(gutterNum)
elements.splice(elements.indexOf(elementRef), 1)

Expand All @@ -88,7 +101,7 @@ export class SplitGutterDirective {
}
}

static ngTemplateContextGuard(dir: SplitGutterDirective, ctx: unknown): ctx is SplitGutterTemplateContext {
static ngTemplateContextGuard(_dir: SplitGutterDirective, ctx: unknown): ctx is SplitGutterTemplateContext {
return true
}
}
5 changes: 4 additions & 1 deletion projects/angular-split/src/lib/split/split.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ export class SplitComponent {
filter(
(context) =>
!this.customGutter() ||
this.customGutter().canStartDragging(context.mouseDownEvent.target as HTMLElement, context.gutterIndex + 1),
this.customGutter()._canStartDragging(
context.mouseDownEvent.target as HTMLElement,
context.gutterIndex + 1,
),
),
switchMap((mouseDownContext) =>
// As we have gutterClickDeltaPx we can't just start the drag but need to make sure
Expand Down
Loading