8
0

theme.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import testUtils from '@ckeditor/ckeditor5-ui/tests/_utils/utils';
  7. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  8. import Model from '@ckeditor/ckeditor5-ui/src/model';
  9. import View from '@ckeditor/ckeditor5-ui/src/view';
  10. import IconView from '@ckeditor/ckeditor5-ui/src/icon/iconview';
  11. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  12. import { createDropdown, addListToDropdown, addToolbarToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
  13. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  14. import ToolbarSeparatorView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarseparatorview';
  15. import InputTextView from '@ckeditor/ckeditor5-ui/src/inputtext/inputtextview';
  16. import LabeledInputView from '@ckeditor/ckeditor5-ui/src/labeledinput/labeledinputview';
  17. import boldIcon from '@ckeditor/ckeditor5-basic-styles/theme/icons/bold.svg';
  18. import italicIcon from '@ckeditor/ckeditor5-basic-styles/theme/icons/italic.svg';
  19. import checkIcon from '@ckeditor/ckeditor5-core/theme/icons/check.svg';
  20. import cancelIcon from '@ckeditor/ckeditor5-core/theme/icons/cancel.svg';
  21. import SplitButtonView from '@ckeditor/ckeditor5-ui/src/dropdown/button/splitbuttonview';
  22. class TextView extends View {
  23. constructor() {
  24. super();
  25. this.element = document.createTextNode( 'Sample text' );
  26. }
  27. }
  28. class ToolbarNewlineView extends View {
  29. constructor() {
  30. super();
  31. this.setTemplate( {
  32. tag: 'span',
  33. attributes: {
  34. class: 'ck-toolbar__newline'
  35. }
  36. } );
  37. }
  38. }
  39. const ui = testUtils.createTestUIView( {
  40. 'iconPlain1': '#icon-plain-1',
  41. 'iconPlain2': '#icon-plain-2',
  42. 'iconColor1': '#icon-color-1',
  43. 'iconColor2': '#icon-color-2',
  44. 'buttonStates': '#button-states',
  45. 'buttonTypes': '#button-types',
  46. 'buttonIcon': '#button-icon',
  47. 'buttonCustom': '#button-custom',
  48. 'buttonIconCustom': '#button-icon-custom',
  49. 'buttonIconStates': '#button-icon-states',
  50. 'buttonResponsive1': '#button-responsive-1',
  51. 'buttonResponsive2': '#button-responsive-2',
  52. 'buttonResponsive3': '#button-responsive-3',
  53. 'buttonTooltip': '#button-tooltip',
  54. listDropdown: '#list-dropdown',
  55. buttonDropdown: '#button-dropdown',
  56. 'toolbarText': '#toolbar-text',
  57. 'toolbarButton': '#toolbar-button',
  58. 'toolbarRounded': '#toolbar-rounded',
  59. 'toolbarWrap': '#toolbar-wrap',
  60. 'toolbarSeparator': '#toolbar-separator',
  61. 'toolbarMultiRow': '#toolbar-multi-row',
  62. 'inputLabeled': '#input-labeled',
  63. 'inputReadOnly': '#input-read-only'
  64. } );
  65. renderIcon();
  66. renderButton();
  67. renderDropdown();
  68. renderToolbar();
  69. renderInput();
  70. function renderIcon() {
  71. // --- In-text ------------------------------------------------------------
  72. ui.iconPlain1.add( icon( boldIcon ) );
  73. ui.iconPlain2.add( icon( italicIcon ) );
  74. ui.iconColor1.add( icon( boldIcon ) );
  75. ui.iconColor2.add( icon( italicIcon ) );
  76. }
  77. function renderButton() {
  78. // --- States ------------------------------------------------------------
  79. ui.buttonStates.add( toolbar( [
  80. button( {
  81. label: 'State: normal (none)',
  82. } ),
  83. button( {
  84. label: 'State: disabled',
  85. isEnabled: false
  86. } ),
  87. button( {
  88. label: 'State: on',
  89. isOn: true
  90. } )
  91. ] ) );
  92. // --- Types ------------------------------------------------------------
  93. const actionButton = button( { label: 'Action button' } );
  94. const roundedButton = button( { label: 'Rounded corners' } );
  95. const boldButton = button( { label: 'Bold text' } );
  96. const saveButton = button( { label: 'Save', withText: false, icon: checkIcon } );
  97. const cancelButton = button( { label: 'Cancel', withText: false, icon: cancelIcon } );
  98. ui.buttonTypes.add( toolbar( [
  99. actionButton, roundedButton, boldButton, saveButton, cancelButton
  100. ] ) );
  101. // TODO: It requires model interface.
  102. actionButton.element.classList.add( 'ck-button-action' );
  103. // TODO: It requires model interface.
  104. roundedButton.element.classList.add( 'ck-rounded-corners' );
  105. // TODO: It requires model interface.
  106. boldButton.element.classList.add( 'ck-button-bold' );
  107. saveButton.element.classList.add( 'ck-button-save' );
  108. cancelButton.element.classList.add( 'ck-button-cancel' );
  109. // --- Icon ------------------------------------------------------------
  110. ui.buttonIcon.add( toolbar( [
  111. button( {
  112. label: 'Bold',
  113. icon: boldIcon
  114. } )
  115. ] ) );
  116. const styledButton = button( {
  117. label: 'Button with an icon and custom styles',
  118. icon: italicIcon
  119. } );
  120. ui.buttonIconCustom.add( toolbar( [ styledButton ] ) );
  121. // TODO: It probably requires model interface.
  122. styledButton.element.setAttribute( 'style', 'border-radius: 100px; border: 0' );
  123. const disabledActionButton = button( {
  124. label: 'Disabled action',
  125. icon: boldIcon,
  126. isEnabled: false
  127. } );
  128. ui.buttonIconStates.add( toolbar( [
  129. button( {
  130. label: 'Disabled',
  131. icon: boldIcon,
  132. isEnabled: false
  133. } ),
  134. disabledActionButton,
  135. button( {
  136. label: 'Bold',
  137. withText: false,
  138. tooltip: true,
  139. icon: boldIcon
  140. } )
  141. ] ) );
  142. // TODO: It requires model interface.
  143. disabledActionButton.element.classList.add( 'ck-button-action' );
  144. // --- Responsive ------------------------------------------------------------
  145. for ( let i = 1; i < 4; i++ ) {
  146. const notextButton = button( {
  147. label: 'Bold',
  148. withText: false,
  149. tooltip: true,
  150. icon: boldIcon
  151. } );
  152. ui[ `buttonResponsive${ i }` ].add( toolbar( [
  153. button( {
  154. label: 'A button',
  155. isEnabled: true
  156. } ),
  157. button( {
  158. label: 'Bold',
  159. icon: boldIcon,
  160. isEnabled: true
  161. } ),
  162. notextButton
  163. ] ) );
  164. // TODO: It requires model interface.
  165. notextButton.element.classList.add( 'ck-button-action' );
  166. }
  167. // --- Tooltip ------------------------------------------------------------
  168. ui.buttonTooltip.add( toolbar( [
  169. button( {
  170. label: 'This button has a tooltip (south)',
  171. withText: true,
  172. tooltip: 'The content of the tooltip',
  173. } ),
  174. button( {
  175. label: 'This one too – north',
  176. withText: true,
  177. keystroke: 'Ctrl+N',
  178. tooltip: true,
  179. tooltipPosition: 'n'
  180. } )
  181. ] ) );
  182. }
  183. function renderDropdown() {
  184. // --- ListDropdown ------------------------------------------------------------
  185. const collection = new Collection( { idProperty: 'label' } );
  186. [ 'Arial', 'Tahoma', 'Georgia' ].forEach( font => {
  187. collection.add( new Model( {
  188. label: font,
  189. style: `font-family: ${ font }`
  190. } ) );
  191. } );
  192. ui.listDropdown.add( toolbar( [
  193. listDropdown( {
  194. label: 'Normal state',
  195. isEnabled: true,
  196. items: collection
  197. } ),
  198. listDropdown( {
  199. label: 'Disabled',
  200. isEnabled: false,
  201. items: collection
  202. } )
  203. ] ) );
  204. ui.buttonDropdown.add( toolbar( [
  205. toolbarDropdown( {
  206. label: 'Normal state',
  207. isEnabled: true,
  208. buttons: [
  209. button( {
  210. withText: false,
  211. label: 'foo',
  212. icon: boldIcon
  213. } ),
  214. button( {
  215. withText: false,
  216. label: 'foo',
  217. icon: italicIcon
  218. } )
  219. ]
  220. } ),
  221. toolbarDropdown( {
  222. label: 'Disabled',
  223. isEnabled: false,
  224. buttons: [
  225. button( {
  226. withText: false,
  227. isEnabled: false,
  228. label: 'foo',
  229. icon: boldIcon
  230. } ),
  231. button( {
  232. withText: false,
  233. isEnabled: false,
  234. label: 'foo',
  235. icon: italicIcon
  236. } )
  237. ]
  238. } )
  239. ] ) );
  240. }
  241. function renderToolbar() {
  242. // --- Text ------------------------------------------------------------
  243. ui.toolbarText.add( toolbar( [
  244. icon( boldIcon ),
  245. text()
  246. ] ) );
  247. // --- Button ------------------------------------------------------------
  248. ui.toolbarButton.add( toolbar( [
  249. button(),
  250. text(),
  251. button( {
  252. label: 'Button with an icon',
  253. icon: boldIcon
  254. } ),
  255. listDropdown(),
  256. splitButtonDropdown( {
  257. label: 'Split button dropdown',
  258. withText: false,
  259. icon: boldIcon
  260. } ),
  261. button()
  262. ] ) );
  263. // --- Rounded ------------------------------------------------------------
  264. ui.toolbarRounded.add( toolbar( [
  265. button( {
  266. label: 'A button which corners are also rounded because of toolbar class'
  267. } ),
  268. button( {
  269. label: 'Button with an icon',
  270. icon: boldIcon
  271. } )
  272. ] ) );
  273. // --- Wrap ------------------------------------------------------------
  274. const wrapToolbar = toolbar( [
  275. button(),
  276. button(),
  277. button()
  278. ] );
  279. ui.toolbarWrap.add( wrapToolbar );
  280. wrapToolbar.element.style.width = '150px';
  281. // --- Separator ------------------------------------------------------------
  282. ui.toolbarSeparator.add( toolbar( [
  283. button(),
  284. button(),
  285. toolbarSeparator(),
  286. button( {
  287. label: 'Foo',
  288. icon: boldIcon
  289. } ),
  290. toolbarSeparator(),
  291. button( {
  292. label: 'Bar RTL',
  293. icon: boldIcon
  294. } )
  295. ] ) );
  296. // --- Multi row ------------------------------------------------------------
  297. ui.toolbarMultiRow.add( toolbar( [
  298. button(),
  299. button(),
  300. toolbarNewLine(),
  301. button( {
  302. label: 'Foo',
  303. icon: boldIcon
  304. } ),
  305. button( {
  306. label: 'Bar',
  307. icon: boldIcon
  308. } ),
  309. button( {
  310. label: 'Baz',
  311. icon: boldIcon
  312. } )
  313. ] ) );
  314. }
  315. function renderInput() {
  316. ui.inputLabeled.add( input() );
  317. ui.inputReadOnly.add( input( {
  318. label: 'A read–only input',
  319. isReadOnly: true,
  320. value: 'Read–only input value'
  321. } ) );
  322. }
  323. function text() {
  324. return new TextView();
  325. }
  326. function icon( content ) {
  327. const icon = new IconView();
  328. icon.content = content;
  329. return icon;
  330. }
  331. function button( {
  332. label = 'Button',
  333. isEnabled = true,
  334. isOn = false,
  335. withText = true,
  336. keystroke = null,
  337. tooltip,
  338. tooltipPosition = 's',
  339. icon
  340. } = {} ) {
  341. const button = new ButtonView();
  342. button.set( { label, isEnabled, isOn, withText, icon, keystroke, tooltip, tooltipPosition } );
  343. return button;
  344. }
  345. function toolbar( children = [] ) {
  346. const toolbar = new ToolbarView();
  347. children.forEach( c => toolbar.items.add( c ) );
  348. return toolbar;
  349. }
  350. function listDropdown( {
  351. label = 'Dropdown',
  352. isEnabled = true,
  353. isOn = false,
  354. withText = true,
  355. items = new Collection( { idProperty: 'label' } )
  356. } = {} ) {
  357. const dropdown = createDropdown( {} );
  358. addListToDropdown( dropdown, items );
  359. dropdown.buttonView.set( { label, isEnabled, isOn, withText } );
  360. return dropdown;
  361. }
  362. function toolbarDropdown( {
  363. label = 'Button dropdown',
  364. isEnabled = true,
  365. isOn = false,
  366. withText = true,
  367. isVertical = true,
  368. buttons = []
  369. } = {} ) {
  370. const dropdown = createDropdown( {} );
  371. addToolbarToDropdown( dropdown, buttons );
  372. dropdown.buttonView.set( { label, isEnabled, isVertical, isOn, withText } );
  373. return dropdown;
  374. }
  375. function splitButtonDropdown( {
  376. label = 'Button dropdown',
  377. icon = undefined,
  378. isEnabled = true,
  379. isOn = false,
  380. withText = true,
  381. isVertical = true,
  382. buttons = []
  383. } = {} ) {
  384. const dropdown = createDropdown( {}, SplitButtonView );
  385. addToolbarToDropdown( dropdown, buttons );
  386. dropdown.buttonView.set( { icon, label, isEnabled, isVertical, isOn, withText } );
  387. return dropdown;
  388. }
  389. function toolbarSeparator() {
  390. return new ToolbarSeparatorView();
  391. }
  392. function toolbarNewLine() {
  393. return new ToolbarNewlineView();
  394. }
  395. function input( {
  396. label = 'Labeled input',
  397. isReadOnly = false,
  398. value = 'The value of the input'
  399. } = {} ) {
  400. const labeledInput = new LabeledInputView( {}, InputTextView );
  401. labeledInput.set( { isReadOnly, label, value } );
  402. return labeledInput;
  403. }