UnicodeBidiService.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @typechecks
  8. *
  9. */
  10. /**
  11. * Stateful API for text direction detection
  12. *
  13. * This class can be used in applications where you need to detect the
  14. * direction of a sequence of text blocks, where each direction shall be used
  15. * as the fallback direction for the next one.
  16. *
  17. * NOTE: A default direction, if not provided, is set based on the global
  18. * direction, as defined by `UnicodeBidiDirection`.
  19. *
  20. * == Example ==
  21. * ```
  22. * var UnicodeBidiService = require('UnicodeBidiService');
  23. *
  24. * var bidiService = new UnicodeBidiService();
  25. *
  26. * ...
  27. *
  28. * bidiService.reset();
  29. * for (var para in paragraphs) {
  30. * var dir = bidiService.getDirection(para);
  31. * ...
  32. * }
  33. * ```
  34. *
  35. * Part of our implementation of Unicode Bidirectional Algorithm (UBA)
  36. * Unicode Standard Annex #9 (UAX9)
  37. * http://www.unicode.org/reports/tr9/
  38. */
  39. 'use strict';
  40. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  41. var UnicodeBidi = require("./UnicodeBidi");
  42. var UnicodeBidiDirection = require("./UnicodeBidiDirection");
  43. var invariant = require("./invariant");
  44. var UnicodeBidiService =
  45. /*#__PURE__*/
  46. function () {
  47. /**
  48. * Stateful class for paragraph direction detection
  49. *
  50. * @param defaultDir Default direction of the service
  51. */
  52. function UnicodeBidiService(defaultDir) {
  53. _defineProperty(this, "_defaultDir", void 0);
  54. _defineProperty(this, "_lastDir", void 0);
  55. if (!defaultDir) {
  56. defaultDir = UnicodeBidiDirection.getGlobalDir();
  57. } else {
  58. !UnicodeBidiDirection.isStrong(defaultDir) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Default direction must be a strong direction (LTR or RTL)') : invariant(false) : void 0;
  59. }
  60. this._defaultDir = defaultDir;
  61. this.reset();
  62. }
  63. /**
  64. * Reset the internal state
  65. *
  66. * Instead of creating a new instance, you can just reset() your instance
  67. * everytime you start a new loop.
  68. */
  69. var _proto = UnicodeBidiService.prototype;
  70. _proto.reset = function reset() {
  71. this._lastDir = this._defaultDir;
  72. };
  73. /**
  74. * Returns the direction of a block of text, and remembers it as the
  75. * fall-back direction for the next paragraph.
  76. *
  77. * @param str A text block, e.g. paragraph, table cell, tag
  78. * @return The resolved direction
  79. */
  80. _proto.getDirection = function getDirection(str) {
  81. this._lastDir = UnicodeBidi.getDirection(str, this._lastDir);
  82. return this._lastDir;
  83. };
  84. return UnicodeBidiService;
  85. }();
  86. module.exports = UnicodeBidiService;