npm package: @ray-js/power-memory-sync-utils
Description: 断电记忆功能页工具包
Latest version: 1.0.0

[简体中文](./README-zh_CN.md) | English

# @ray-js/power-memory-sync-utils

[![latest](https://img.shields.io/npm/v/@ray-js/power-memory-sync-utils/latest.svg)](https://www.npmjs.com/package/@ray-js/power-memory-sync-utils) [![download](https://img.shields.io/npm/dt/@ray-js/power-memory-sync-utils.svg)](https://www.npmjs.com/package/@ray-js/power-memory-sync-utils)

> Power-off memory function page toolkit

## Installation

```sh
$ npm install @ray-js/power-memory-sync-utils
// Or
$ yarn add @ray-js/power-memory-sync-utils
```

## Development

```sh
# Compile demo code in real time
yarn start:tuya
```

## Usage

### Navigate to functional page

```typescript
import {
  navigateToPowerMemoryFunctional,
  PowerMemoryFunctionalData,
  clearPowerMemoryFunctionalData,
} from '@ray-js/power-memory-sync-utils';

// LampPowerMemoryFunctional is the functional page name configured in global.config.ts
const jumpUrl = `functional://LampPowerMemoryFunctional/home?deviceId=${deviceId || ''}&groupId=${
  groupId || ''
}`;
// Preset data
const data: PowerMemoryFunctionalData = {
  collectColors: [{ hue: 100, saturation: 1000, value: 1000 }],
  collectWhites: [{ brightness: 1000, temperature: 1000 }],
};

// Navigate
navigateToPowerMemoryFunctional(jumpUrl, data);

// Clear data after returning to the current mini program
clearPowerMemoryFunctionalData(jumpUrl);
```

### Listen for functional page return

```ts
import {
  onPowerMemoryFunctionalChange,
  offPowerMemoryFunctionalChange,
} from '@ray-js/power-memory-sync-utils';

useEffect(() => {
  onPowerMemoryFunctionalChange(res => {
    console.log(res, ' ---Functional page return');
  });
  return offPowerMemoryFunctionalChange;
}, []);
```

## API

| Method Name                     | Parameter Description                             | Type                                                                                                                        |
| ------------------------------- | ------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| presetPowerMemoryFunctionalData | Store functional page data as Promise             | `(url: string, data: PowerMemoryFunctionalData) => Promise<boolean>`                                                        |
| clearPowerMemoryFunctionalData  | Clear data                                        | `(url: string) => Promise<boolean>`                                                                                         |
| navigateToPowerMemoryFunctional | Navigate to function page                         | `(url: string, data?: PowerMemoryFunctionalData) => void`                                                                   |
| onPowerMemoryFunctionalChange   | Register listener for functional page return data | `(func: (res: {id: "LampPowerMemoryFunctional";type: "cancel" \| "save";data: PowerMemoryFunctionalRes;}) => void) => void` |
| offPowerMemoryFunctionalChange  | Remove listener for functional page return data   | `() => void`                                                                                                                |
| sendPowerMemoryFunctionalData   | Send functional page return data                  | ` (type: "cancel" \| "save", data: PowerMemoryFunctionalRes) => void`                                                       |

### PowerMemoryFunctionalData

| Parameter     | Parameter Description         | Type                   | Default Value                                                                                                                             | Required |
| ------------- | ----------------------------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| customColor   | Custom power-off memory value | `Partial<CustomColor>` | `{ colorMode: 'white',hue: 0,saturation: 0,value: 0,brightness: 1000,temperature: 1000 }`                                                 | No       |
| collectColors | Scene color collection list   | `ColourCustom[]`       | `[ { hue: 0, saturation: 1000, value: 1000 }, { hue: 120, saturation: 1000, value: 1000 }, { hue: 240, saturation: 1000, value: 1000 }];` | No       |
| collectWhites | White color collection list   | `WhiteCustom[]`        | `[ { temperature: 0, brightness: 1000 }, { temperature: 500, brightness: 1000 }, { temperature: 1000, brightness: 1000 }]`                | No       |

#### CustomColor

| Parameter   | Parameter Description                  | Type                | Required |
| ----------- | -------------------------------------- | ------------------- | -------- |
| colorMode   | Mode - white or colored light          | `white` \| `colour` | No       |
| hue         | Colored light - hue value 0-360        | `number`            | No       |
| saturation  | Colored light - saturation 0-1000      | `number`            | No       |
| value       | Colored light - brightness 0-1000      | `number`            | No       |
| brightness  | White light - brightness 1-1000        | `number`            | No       |
| temperature | White light - color temperature 0-1000 | `number`            | No       |

#### ColourCustom

| Parameter  | Parameter Description             | Type     | Required |
| ---------- | --------------------------------- | -------- | -------- |
| hue        | Colored light - hue value 0-360   | `number` | No       |
| saturation | Colored light - saturation 0-1000 | `number` | No       |
| value      | Colored light - brightness 0-1000 | `number` | No       |

#### WhiteCustom

| Parameter   | Parameter Description                  | Type     | Required |
| ----------- | -------------------------------------- | -------- | -------- |
| brightness  | White light - brightness 1-1000        | `number` | No       |
| temperature | White light - color temperature 0-1000 | `number` | No       |