trumbowyg.lineheight.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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': 'Petite',
  28. 'normal': 'Normale',
  29. '1.5': 'Grande',
  30. '2.0': 'Très grande'
  31. }
  32. },
  33. hu: {
  34. lineheight: 'Line height',
  35. lineheights: {
  36. '0.9': 'Small',
  37. 'normal': 'Regular',
  38. '1.5': 'Large',
  39. '2.0': 'Extra large'
  40. }
  41. },
  42. it: {
  43. lineheight: 'Altezza linea',
  44. lineheights: {
  45. '0.9': 'Bassa',
  46. 'normal': 'Normale',
  47. '1.5': 'Alta',
  48. '2.0': 'Molto alta'
  49. }
  50. },
  51. ko: {
  52. lineheight: '줄 간격',
  53. lineheights: {
  54. '0.9': '좁게',
  55. 'normal': '보통',
  56. '1.5': '넓게',
  57. '2.0': '아주 넓게'
  58. }
  59. },
  60. nl: {
  61. lineheight: 'Regelhoogte',
  62. lineheights: {
  63. '0.9': 'Klein',
  64. 'normal': 'Normaal',
  65. '1.5': 'Groot',
  66. '2.0': 'Extra groot'
  67. }
  68. },
  69. pt_br: {
  70. lineheight: 'Altura de linha',
  71. lineheights: {
  72. '0.9': 'Pequena',
  73. 'normal': 'Regular',
  74. '1.5': 'Grande',
  75. '2.0': 'Extra grande'
  76. }
  77. },
  78. tr: {
  79. lineheight: 'Satır yüksekliği',
  80. lineheights: {
  81. '0.9': 'Küçük',
  82. 'normal': 'Normal',
  83. '1.5': 'Büyük',
  84. '2.0': 'Çok Büyük'
  85. }
  86. },
  87. zh_tw: {
  88. lineheight: '文字間距',
  89. lineheights: {
  90. '0.9': '小',
  91. 'normal': '正常',
  92. '1.5': '大',
  93. '2.0': '特大'
  94. }
  95. },
  96. }
  97. });
  98. // jshint camelcase:true
  99. var defaultOptions = {
  100. sizeList: [
  101. '0.9',
  102. 'normal',
  103. '1.5',
  104. '2.0'
  105. ]
  106. };
  107. // Add dropdown with font sizes
  108. $.extend(true, $.trumbowyg, {
  109. plugins: {
  110. lineheight: {
  111. init: function (trumbowyg) {
  112. trumbowyg.o.plugins.lineheight = $.extend({},
  113. defaultOptions,
  114. trumbowyg.o.plugins.lineheight || {}
  115. );
  116. trumbowyg.addBtnDef('lineheight', {
  117. dropdown: buildDropdown(trumbowyg)
  118. });
  119. }
  120. }
  121. }
  122. });
  123. // Build the dropdown
  124. function buildDropdown(trumbowyg) {
  125. var dropdown = [];
  126. $.each(trumbowyg.o.plugins.lineheight.sizeList, function(index, size) {
  127. trumbowyg.addBtnDef('lineheight_' + size, {
  128. text: trumbowyg.lang.lineheights[size] || size,
  129. hasIcon: false,
  130. fn: function(){
  131. trumbowyg.saveRange();
  132. var text = trumbowyg.getRangeText();
  133. if (text.replace(/\s/g, '') !== '') {
  134. try {
  135. var parent = getSelectionParentElement();
  136. $(parent).css('lineHeight', size);
  137. } catch (e) {
  138. }
  139. }
  140. }
  141. });
  142. dropdown.push('lineheight_' + size);
  143. });
  144. return dropdown;
  145. }
  146. // Get the selection's parent
  147. function getSelectionParentElement() {
  148. var parentEl = null,
  149. selection;
  150. if (window.getSelection) {
  151. selection = window.getSelection();
  152. if (selection.rangeCount) {
  153. parentEl = selection.getRangeAt(0).commonAncestorContainer;
  154. if (parentEl.nodeType !== 1) {
  155. parentEl = parentEl.parentNode;
  156. }
  157. }
  158. } else if ((selection = document.selection) && selection.type !== 'Control') {
  159. parentEl = selection.createRange().parentElement();
  160. }
  161. return parentEl;
  162. }
  163. })(jQuery);