UnicodeBidiDirection.js.flow 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * @providesModule UnicodeBidiDirection
  8. * @typechecks
  9. * @flow
  10. */
  11. /**
  12. * Constants to represent text directionality
  13. *
  14. * Also defines a *global* direciton, to be used in bidi algorithms as a
  15. * default fallback direciton, when no better direction is found or provided.
  16. *
  17. * NOTE: Use `setGlobalDir()`, or update `initGlobalDir()`, to set the initial
  18. * global direction value based on the application.
  19. *
  20. * Part of the implementation of Unicode Bidirectional Algorithm (UBA)
  21. * Unicode Standard Annex #9 (UAX9)
  22. * http://www.unicode.org/reports/tr9/
  23. */
  24. 'use strict';
  25. const invariant = require("./invariant");
  26. export type BidiDirection = 'LTR' | 'RTL' | 'NEUTRAL';
  27. export type HTMLDir = 'ltr' | 'rtl';
  28. const NEUTRAL = 'NEUTRAL'; // No strong direction
  29. const LTR = 'LTR'; // Left-to-Right direction
  30. const RTL = 'RTL'; // Right-to-Left direction
  31. let globalDir: ?BidiDirection = null; // == Helpers ==
  32. /**
  33. * Check if a directionality value is a Strong one
  34. */
  35. function isStrong(dir: BidiDirection): boolean {
  36. return dir === LTR || dir === RTL;
  37. }
  38. /**
  39. * Get string value to be used for `dir` HTML attribute or `direction` CSS
  40. * property.
  41. */
  42. function getHTMLDir(dir: BidiDirection): HTMLDir {
  43. invariant(isStrong(dir), '`dir` must be a strong direction to be converted to HTML Direction');
  44. return dir === LTR ? 'ltr' : 'rtl';
  45. }
  46. /**
  47. * Get string value to be used for `dir` HTML attribute or `direction` CSS
  48. * property, but returns null if `dir` has same value as `otherDir`.
  49. * `null`.
  50. */
  51. function getHTMLDirIfDifferent(dir: BidiDirection, otherDir: BidiDirection): ?HTMLDir {
  52. invariant(isStrong(dir), '`dir` must be a strong direction to be converted to HTML Direction');
  53. invariant(isStrong(otherDir), '`otherDir` must be a strong direction to be converted to HTML Direction');
  54. return dir === otherDir ? null : getHTMLDir(dir);
  55. } // == Global Direction ==
  56. /**
  57. * Set the global direction.
  58. */
  59. function setGlobalDir(dir: BidiDirection): void {
  60. globalDir = dir;
  61. }
  62. /**
  63. * Initialize the global direction
  64. */
  65. function initGlobalDir(): void {
  66. setGlobalDir(LTR);
  67. }
  68. /**
  69. * Get the global direction
  70. */
  71. function getGlobalDir(): BidiDirection {
  72. if (!globalDir) {
  73. this.initGlobalDir();
  74. }
  75. invariant(globalDir, 'Global direction not set.');
  76. return globalDir;
  77. }
  78. const UnicodeBidiDirection = {
  79. // Values
  80. NEUTRAL,
  81. LTR,
  82. RTL,
  83. // Helpers
  84. isStrong,
  85. getHTMLDir,
  86. getHTMLDirIfDifferent,
  87. // Global Direction
  88. setGlobalDir,
  89. initGlobalDir,
  90. getGlobalDir
  91. };
  92. module.exports = UnicodeBidiDirection;