addce8a1142a97773bbfa3c1c0afa9408a94b8b016f70574f39e7fb6f8118d40d874b3d16dfc9d969e1fbb2796d298eef1d3f6c81c2c9cc5c758e5fa5f4f96 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { Emitter } from '../../../../base/common/event.js';
  6. export class ColorPickerModel {
  7. constructor(color, availableColorPresentations, presentationIndex) {
  8. this.presentationIndex = presentationIndex;
  9. this._onColorFlushed = new Emitter();
  10. this.onColorFlushed = this._onColorFlushed.event;
  11. this._onDidChangeColor = new Emitter();
  12. this.onDidChangeColor = this._onDidChangeColor.event;
  13. this._onDidChangePresentation = new Emitter();
  14. this.onDidChangePresentation = this._onDidChangePresentation.event;
  15. this.originalColor = color;
  16. this._color = color;
  17. this._colorPresentations = availableColorPresentations;
  18. }
  19. get color() {
  20. return this._color;
  21. }
  22. set color(color) {
  23. if (this._color.equals(color)) {
  24. return;
  25. }
  26. this._color = color;
  27. this._onDidChangeColor.fire(color);
  28. }
  29. get presentation() { return this.colorPresentations[this.presentationIndex]; }
  30. get colorPresentations() {
  31. return this._colorPresentations;
  32. }
  33. set colorPresentations(colorPresentations) {
  34. this._colorPresentations = colorPresentations;
  35. if (this.presentationIndex > colorPresentations.length - 1) {
  36. this.presentationIndex = 0;
  37. }
  38. this._onDidChangePresentation.fire(this.presentation);
  39. }
  40. selectNextColorPresentation() {
  41. this.presentationIndex = (this.presentationIndex + 1) % this.colorPresentations.length;
  42. this.flushColor();
  43. this._onDidChangePresentation.fire(this.presentation);
  44. }
  45. guessColorPresentation(color, originalText) {
  46. for (let i = 0; i < this.colorPresentations.length; i++) {
  47. if (originalText.toLowerCase() === this.colorPresentations[i].label) {
  48. this.presentationIndex = i;
  49. this._onDidChangePresentation.fire(this.presentation);
  50. break;
  51. }
  52. }
  53. }
  54. flushColor() {
  55. this._onColorFlushed.fire(this._color);
  56. }
  57. }