button.yml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. properties:
  2. - name: "name"
  3. type: "String"
  4. default: "''"
  5. description: |
  6. 该按钮组件的名称,会在按钮的 DOM 元素上增加相应的 class `.toolbar-item-[name]`,并且会用于在 Toolbar 注册,初始化编辑器时,可以在 [toolbar](../docs/doc-config.html#anchor-toolbar) 中配置初始化按钮组件,所以此名称应该区别于其他按钮。
  7. ```coffee
  8. class HelloButton extends SimditorButton
  9. name: 'hello'
  10. editor = new Simditor
  11. toolbar: ['hello']
  12. ```
  13. - name: "icon"
  14. type: "String"
  15. default: "''"
  16. description: |
  17. 按钮显示的图标,Simditor 使用 [Fonticons](http://www.fonticons.com/) 的服务来管理按钮图标,如果该属性为空则显示按钮的 **text** 属性。
  18. ```coffee
  19. # 按钮图标为 .icon-bold
  20. class HelloButton extends SimditorButton
  21. name: 'hello'
  22. icon: 'bold'
  23. ```
  24. - name: "title"
  25. type: "String"
  26. default: "''"
  27. description: |
  28. 按钮 DOM 元素的 title 属性,鼠标 hover 的时候显示。
  29. - name: "text"
  30. type: "String"
  31. default: "''"
  32. description: |
  33. 非图标按钮,用此 **text** 作文字按钮。
  34. - name: "htmlTag"
  35. type: "String"
  36. default: "''"
  37. description: |
  38. 单个或多个由逗号分隔的 HTML 标签名称,表示按钮会在编辑器中插入的 HTML 标签,可用于 [status](#anchor-status) 方法中识别按钮的激活状态。
  39. ```coffee
  40. class TitleButton extends SimditorButton
  41. name: 'title'
  42. title: '标题文字'
  43. htmlTag: 'h1, h2, h3, h4'
  44. ```
  45. - name: "disableTag"
  46. type: "String"
  47. default: "''"
  48. description: |
  49. 单个或多个由逗号分隔的 HTML 标签名称,当光标在这些元素中时,按钮会被禁用,比如在代码和表格中不能使用标题按钮。
  50. ```coffee
  51. class TitleButton extends SimditorButton
  52. name: 'title'
  53. title: '标题文字'
  54. htmlTag: 'h1, h2, h3, h4'
  55. disableTag: 'pre, table'
  56. ```
  57. - name: "menu"
  58. type: "Boolean / Array"
  59. default: "false"
  60. description: |
  61. 配置该属性,按钮会成为菜单按钮。
  62. ```coffee
  63. class TitleButton extends SimditorButton
  64. name: 'title'
  65. title: '标题文字'
  66. htmlTag: 'h1'
  67. disableTag: 'pre, table'
  68. menu: [{
  69. name: 'normal'
  70. title: '普通文本'
  71. text: '普通文本'
  72. param: 'p'
  73. }, '|', {
  74. name: 'h1',
  75. title: '标题 1'
  76. text: '标题 1'
  77. param: 'h1'
  78. }]
  79. ```
  80. **menu** 数组中可能是对象或者字符串 `'|'`,后者表示菜单中的分隔线,如果是对象,则有四个属性:`name``title``text``param`,name 用于增加对应的 class,text 和 title 的作用和按钮的属性类似,param 会作为参数传给方法 [command](#anchor-command)。
  81. - name: "active"
  82. type: "Boolean"
  83. default: "false"
  84. description: |
  85. 按钮是否处于激活状态。只读,请使用 [setActive](#anchor-setActive) 方法来设置按钮的激活状态。
  86. - name: "disabled"
  87. type: "Boolean"
  88. default: "false"
  89. description: |
  90. 按钮是否处于禁用状态。只读,请使用 [setDisabled](#anchor-setActive) 方法来设置按钮的激活状态。
  91. - name: "needFocus"
  92. type: "Boolean"
  93. default: "true"
  94. description: |
  95. 如果为 `true` 则表示该按钮必须在鼠标 focus 到编辑器的状态才有效。
  96. - name: "shortcut"
  97. type: "null / String"
  98. default: "null"
  99. description: |
  100. 为按钮设置快捷键,结构为功能键和按键的编码用加号连接,比如 `'cmd+99'`,支持的功能键为 `cmd``shift``alt``ctrl`
  101. ```coffee
  102. class BoldButton extends Button
  103. name: 'bold'
  104. title: '加粗文字'
  105. ...
  106. # cmd + b 会对选中文字加粗
  107. shortcut: 'cmd+66'
  108. ```
  109. methods:
  110. - name: "setActive"
  111. params:
  112. - name: "active"
  113. type: "Boolean"
  114. description: |
  115. 根据参数设置按钮的激活状态,Button 类做了基本的实现,包括设置 active 属性和按钮样式。
  116. 当编辑器 blur 的时候该方法会被触发
  117. ```coffee
  118. setActive: (active) ->
  119. @active = active
  120. @el.toggleClass('active', @active)
  121. ```
  122. - name: "setDisabled"
  123. params:
  124. - name: "disabled"
  125. type: "Boolean"
  126. description: |
  127. 根据参数设置按钮的是否禁用,Button 类做了基本的实现,包括设置 disabled 属性和按钮样式
  128. 当编辑器 blur 的时候该方法会被触发
  129. ```coffee
  130. setDisabled: (disabled) ->
  131. @disabled = disabled
  132. @el.toggleClass('disabled', @disabled)
  133. ```
  134. - name: "status"
  135. params:
  136. - name: "$node"
  137. type: "jQuery Object"
  138. description: |
  139. 该方法在编辑器 [selectionchanged](../docs/doc-event.html#anchor-selectionchanged) 和 [focus](../docs/doc-event.html#anchor-focus) 发生时,在 Toolbar 类中被多次调用,去更新按钮的状态。
  140. 参数 $node 是光标所在位置冒泡遍历所有的父元素直到编辑区域的最外层 DOM,也就是 editor.body,但不包括它。
  141. 所以可以根据参数 $node 和属性 htmlTag disableTag 来更新按钮状态。
  142. 下面是 Button 类的基本实现
  143. ```coffee
  144. status: ($node) ->
  145. @setDisabled $node.is(@disableTag) if $node?
  146. return true if @disabled
  147. @setActive $node.is(@htmlTag) if $node?
  148. @active
  149. ```
  150. - name: "command"
  151. params:
  152. - name: "param"
  153. type: "any"
  154. description: |
  155. 点击按钮会执行的操作,通常是修改编辑的内容。在 Button 类中这是一个抽象方法,需要各按钮组件自己实现。
  156. 如果是菜单按钮,则参数 param 会是 [menu](#anchor-menu) 配置中的 param
  157. ```coffee
  158. class BoldButton extends Button
  159. ...
  160. command: ->
  161. document.execCommand 'bold'
  162. @editor.trigger 'valuechanged'
  163. @editor.trigger 'selectionchanged'
  164. ```
  165. 需要注意的是,如果内容改变了,需要手动触发编辑器的 valuechanged 和 selectionchanged 事件。