UnicodeBidiService.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  41. var UnicodeBidi = require('./UnicodeBidi');
  42. var UnicodeBidiDirection = require('./UnicodeBidiDirection');
  43. var invariant = require('./invariant');
  44. var UnicodeBidiService = function () {
  45. /**
  46. * Stateful class for paragraph direction detection
  47. *
  48. * @param defaultDir Default direction of the service
  49. */
  50. function UnicodeBidiService(defaultDir) {
  51. _classCallCheck(this, UnicodeBidiService);
  52. if (!defaultDir) {
  53. defaultDir = UnicodeBidiDirection.getGlobalDir();
  54. } else {
  55. !UnicodeBidiDirection.isStrong(defaultDir) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Default direction must be a strong direction (LTR or RTL)') : invariant(false) : void 0;
  56. }
  57. this._defaultDir = defaultDir;
  58. this.reset();
  59. }
  60. /**
  61. * Reset the internal state
  62. *
  63. * Instead of creating a new instance, you can just reset() your instance
  64. * everytime you start a new loop.
  65. */
  66. UnicodeBidiService.prototype.reset = function reset() {
  67. this._lastDir = this._defaultDir;
  68. };
  69. /**
  70. * Returns the direction of a block of text, and remembers it as the
  71. * fall-back direction for the next paragraph.
  72. *
  73. * @param str A text block, e.g. paragraph, table cell, tag
  74. * @return The resolved direction
  75. */
  76. UnicodeBidiService.prototype.getDirection = function getDirection(str) {
  77. this._lastDir = UnicodeBidi.getDirection(str, this._lastDir);
  78. return this._lastDir;
  79. };
  80. return UnicodeBidiService;
  81. }();
  82. module.exports = UnicodeBidiService;