Skip to content

Commit

Permalink
✨ Add layer().notification()
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-liu committed May 10, 2024
1 parent 98b1919 commit e2fc3e3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/config/layer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,27 @@ test('layer().modifier(??)', () => {
.build(),
).toThrow()
})

test('layer() notification', () => {
const rule = layer('a').notification(true).build()
const manipulators = rule.manipulators as BasicManipulator[]
expect(manipulators.length).toBe(1)
expect(manipulators[0].to?.[1]).toEqual({
set_notification_message: {
id: 'layer-layer-a',
text: 'Layer - layer-a',
},
})
expect(manipulators[0].to_after_key_up?.[1]).toEqual({
set_notification_message: { id: 'layer-layer-a', text: '' },
})

const ruleB = layer('a').notification('test-b').build()
const manipulatorB = ruleB.manipulators[0] as BasicManipulator
expect(manipulatorB.to?.[1]).toEqual({
set_notification_message: {
id: 'layer-layer-a',
text: 'test-b',
},
})
})
20 changes: 19 additions & 1 deletion src/config/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
SideModifierAlias,
} from './modifier.ts'
import { BasicRuleBuilder } from './rule.ts'
import { toSetVar } from './to.ts'
import { toRemoveNotificationMessage, toSetVar } from './to.ts'

export type LayerKeyCode = Exclude<
FromKeyCode,
Expand Down Expand Up @@ -92,6 +92,8 @@ export class LayerRuleBuilder extends BasicRuleBuilder {

private layerModifiers?: FromModifiers

private layerNotification?: boolean | string

constructor(
key: LayerKeyParam | LayerKeyParam[],
varName?: string,
Expand Down Expand Up @@ -149,6 +151,12 @@ export class LayerRuleBuilder extends BasicRuleBuilder {
return this
}

/** Set the notification when the layer is active. */
public notification(v: boolean | string) {
this.layerNotification = v
return this
}

public build(context?: BuildContext): Rule {
const rule = super.build(context)

Expand Down Expand Up @@ -178,6 +186,9 @@ export class LayerRuleBuilder extends BasicRuleBuilder {
context,
this.layerKeyManipulator,
this.replaceLayerKeyToIfAlone,
this.layerNotification === true
? this.ruleDescription
: this.layerNotification || undefined,
),
...rule.manipulators,
]
Expand Down Expand Up @@ -224,6 +235,7 @@ export function layerToggleManipulator(
context?: BuildContext,
layerKeyManipulator?: BasicManipulatorBuilder,
replaceLayerKeyToIfAlone?: boolean,
notification?: string,
) {
function mergeManipulator<T extends BasicManipulator | BasicManipulator[]>(
to: T,
Expand Down Expand Up @@ -271,6 +283,12 @@ export function layerToggleManipulator(
.toIfAlone({ key_code })
.condition(ifVar(varName, onValue).unless())
if (conditions?.length) manipulator.condition(...conditions)
if (notification) {
const id = `layer-${varName}`
manipulator
.toNotificationMessage(id, notification)
.toAfterKeyUp(toRemoveNotificationMessage(id))
}
if (!context) return mergeManipulator(manipulator.build())

const key = [
Expand Down

0 comments on commit e2fc3e3

Please sign in to comment.