* Update covers. * @param coverConfigList * If coverConfigList is null/undefined, all covers removed.
(coverConfigList: BrushCoverConfig[])
| 377 | * If coverConfigList is null/undefined, all covers removed. |
| 378 | */ |
| 379 | updateCovers(coverConfigList: BrushCoverConfig[]) { |
| 380 | if (__DEV__) { |
| 381 | assert(this._mounted); |
| 382 | } |
| 383 | |
| 384 | coverConfigList = map(coverConfigList, function (coverConfig) { |
| 385 | return merge(clone(DEFAULT_BRUSH_OPT), coverConfig, true); |
| 386 | }) as BrushCoverConfig[]; |
| 387 | |
| 388 | const tmpIdPrefix = '\0-brush-index-'; |
| 389 | const oldCovers = this._covers; |
| 390 | const newCovers = this._covers = [] as BrushCover[]; |
| 391 | const controller = this; |
| 392 | const creatingCover = this._creatingCover; |
| 393 | |
| 394 | (new DataDiffer(oldCovers, coverConfigList, oldGetKey, getKey)) |
| 395 | .add(addOrUpdate) |
| 396 | .update(addOrUpdate) |
| 397 | .remove(remove) |
| 398 | .execute(); |
| 399 | |
| 400 | return this; |
| 401 | |
| 402 | function getKey(brushOption: BrushCoverConfig, index: number): string { |
| 403 | return (brushOption.id != null ? brushOption.id : tmpIdPrefix + index) |
| 404 | + '-' + brushOption.brushType; |
| 405 | } |
| 406 | |
| 407 | function oldGetKey(cover: BrushCover, index: number): string { |
| 408 | return getKey(cover.__brushOption, index); |
| 409 | } |
| 410 | |
| 411 | function addOrUpdate(newIndex: number, oldIndex?: number): void { |
| 412 | const newBrushInternal = coverConfigList[newIndex]; |
| 413 | // Consider setOption in event listener of brushSelect, |
| 414 | // where updating cover when creating should be forbidden. |
| 415 | if (oldIndex != null && oldCovers[oldIndex] === creatingCover) { |
| 416 | newCovers[newIndex] = oldCovers[oldIndex]; |
| 417 | } |
| 418 | else { |
| 419 | const cover = newCovers[newIndex] = oldIndex != null |
| 420 | ? ( |
| 421 | oldCovers[oldIndex].__brushOption = newBrushInternal, |
| 422 | oldCovers[oldIndex] |
| 423 | ) |
| 424 | : endCreating(controller, createCover(controller, newBrushInternal)); |
| 425 | updateCoverAfterCreation(controller, cover); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | function remove(oldIndex: number) { |
| 430 | if (oldCovers[oldIndex] !== creatingCover) { |
| 431 | controller.group.remove(oldCovers[oldIndex]); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | unmount() { |