trumbowyg.lineheight.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. (function ($) {
  2. 'use strict';
  3. $.extend(true, $.trumbowyg, {
  4. langs: {
  5. // jshint camelcase:false
  6. en: {
  7. lineheight: 'Line height',
  8. lineheights: {
  9. '0.9': 'Small',
  10. 'normal': 'Regular',
  11. '1.5': 'Large',
  12. '2.0': 'Extra large'
  13. }
  14. },
  15. da: {
  16. lineheight: 'Linjehøjde',
  17. lineheights: {
  18. '0.9': 'Lille',
  19. 'normal': 'Normal',
  20. '1.5': 'Stor',
  21. '2.0': 'Ekstra stor'
  22. }
  23. },
  24. fr: {
  25. lineheight: 'Hauteur de ligne',
  26. lineheights: {
  27. '0.9': 'Petit',
  28. 'normal': 'Regular',
  29. '1.5': 'Grand',
  30. '2.0': 'Très grand'
  31. }
  32. },
  33. nl: {
  34. lineheight: 'Regelhoogte',
  35. lineheights: {
  36. '0.9': 'Klein',
  37. 'normal': 'Normaal',
  38. '1.5': 'Groot',
  39. '2.0': 'Extra groot'
  40. }
  41. },
  42. tr: {
  43. lineheight: 'Satır yüksekliği',
  44. lineheights: {
  45. '0.9': 'Küçük',
  46. 'normal': 'Normal',
  47. '1.5': 'Büyük',
  48. '2.0': 'Çok Büyük'
  49. }
  50. },
  51. zh_tw: {
  52. lineheight: '文字間距',
  53. lineheights: {
  54. '0.9': '小',
  55. 'normal': '正常',
  56. '1.5': '大',
  57. '2.0': '特大'
  58. }
  59. },
  60. }
  61. });
  62. // jshint camelcase:true
  63. // Add dropdown with font sizes
  64. $.extend(true, $.trumbowyg, {
  65. plugins: {
  66. lineheight: {
  67. init: function (trumbowyg) {
  68. trumbowyg.addBtnDef('lineheight', {
  69. dropdown: buildDropdown(trumbowyg)
  70. });
  71. }
  72. }
  73. }
  74. });
  75. // Build the dropdown
  76. function buildDropdown(trumbowyg) {
  77. var dropdown = [];
  78. var sizes = ['0.9', 'normal', '1.5', '2.0'];
  79. $.each(sizes, function(index, size) {
  80. trumbowyg.addBtnDef('lineheight_' + size, {
  81. text: '<span style="line-height: ' + size + ';">' + trumbowyg.lang.lineheights[size] + '</span>',
  82. hasIcon: false,
  83. fn: function(){
  84. trumbowyg.saveRange();
  85. var text = trumbowyg.getRangeText();
  86. if (text.replace(/\s/g, '') !== '') {
  87. try {
  88. var parent = getSelectionParentElement();
  89. $(parent).css('lineHeight', size);
  90. } catch (e) {
  91. }
  92. }
  93. }
  94. });
  95. dropdown.push('lineheight_' + size);
  96. });
  97. return dropdown;
  98. }
  99. // Get the selection's parent
  100. function getSelectionParentElement() {
  101. var parentEl = null,
  102. selection;
  103. if (window.getSelection) {
  104. selection = window.getSelection();
  105. if (selection.rangeCount) {
  106. parentEl = selection.getRangeAt(0).commonAncestorContainer;
  107. if (parentEl.nodeType !== 1) {
  108. parentEl = parentEl.parentNode;
  109. }
  110. }
  111. } else if ((selection = document.selection) && selection.type !== 'Control') {
  112. parentEl = selection.createRange().parentElement();
  113. }
  114. return parentEl;
  115. }
  116. })(jQuery);