editor.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. 编辑器(Editor) API
  2. ========================================================
  3. .. contents::
  4. :depth: 2
  5. .. index:: create
  6. .. _K.create:
  7. K.create(expr [, options])
  8. --------------------------------------------------------
  9. 创建编辑器,返回第一个KEditor对象。4.1版本开始expr支持多个textarea,之前版本只在第一个textarea上创建。
  10. 创建编辑器后可以用 KindEditor.instances 数组取得已创建的所有KEditor对象。
  11. * 参数:
  12. * mixed expr: element或选择器
  13. * object options: :doc:`option`
  14. * 返回: KEditor
  15. 示例:
  16. .. sourcecode:: js
  17. // 1
  18. // editor 等于 KindEditor.instances[0]
  19. editor = K.create('textarea[name="content"]');
  20. editor.html('HTML code');
  21. // 2
  22. editor = K.create('#editor_id', {
  23. filterMode : true,
  24. langType : 'en'
  25. });
  26. .. note ::
  27. 4.1.2版本开始expr可以直接传入jQuery对象。
  28. .. index:: remove
  29. .. _K.remove:
  30. K.remove(expr)
  31. --------------------------------------------------------
  32. 移除多个编辑器。
  33. * 参数:
  34. * mixed expr: element或选择器
  35. * 返回: undefined
  36. 示例:
  37. .. sourcecode:: js
  38. // 移除ID为editor_id的编辑器
  39. K.remove('#editor_id');
  40. // 移除class为editor-class的编辑器
  41. K.remove('.editor-class');
  42. .. note ::
  43. 4.1.2版本开始支持。
  44. .. index:: sync
  45. .. _K.sync:
  46. K.sync(expr)
  47. --------------------------------------------------------
  48. 将多个编辑器的内容设置到原来的textarea控件里。。
  49. * 参数:
  50. * mixed expr: element或选择器
  51. * 返回: undefined
  52. 示例:
  53. .. sourcecode:: js
  54. // 同步ID为editor_id的编辑器
  55. K.sync('#editor_id');
  56. // 同步class为editor的编辑器
  57. K.sync('.editor');
  58. .. note ::
  59. 4.1.2版本开始支持。
  60. .. index:: html
  61. .. _K.html:
  62. K.html(expr, val)
  63. --------------------------------------------------------
  64. 设置多个编辑器的HTML内容。
  65. * 参数:
  66. * mixed expr: element或选择器
  67. * string val: HTML内容
  68. * 返回: undefined
  69. 示例:
  70. .. sourcecode:: js
  71. K.html('#editor_id', '<div>HTML</div>');
  72. .. note ::
  73. 4.1.8版本开始支持。
  74. .. index:: appendHtml
  75. .. _K.appendHtml:
  76. K.appendHtml(expr, val)
  77. --------------------------------------------------------
  78. 将指定的HTML内容添加到多个编辑器的最后位置。
  79. * 参数:
  80. * mixed expr: element或选择器
  81. * string val: 内容
  82. * 返回: undefined
  83. 示例:
  84. .. sourcecode:: js
  85. K.appendHtml('#editor_id', '<div>HTML</div>');
  86. .. note ::
  87. 4.1.8版本开始支持。
  88. .. index:: insertHtml
  89. .. _K.insertHtml:
  90. K.insertHtml(expr, val)
  91. --------------------------------------------------------
  92. 将指定的HTML内容插入到多个编辑器的光标处。
  93. * 参数:
  94. * mixed expr: element或选择器
  95. * string val: 内容
  96. * 返回: undefined
  97. 示例:
  98. .. sourcecode:: js
  99. K.insertHtml('#editor_id', '<strong>HTML</strong>');
  100. .. note ::
  101. 4.1.8版本开始支持。
  102. .. index:: remove
  103. .. _KEditor.remove:
  104. remove()
  105. --------------------------------------------------------
  106. 移除编辑器。
  107. * 参数: 无
  108. * 返回: KEditor
  109. 示例:
  110. .. sourcecode:: js
  111. editor.remove();
  112. .. index:: html
  113. .. _KEditor.html:
  114. html()
  115. --------------------------------------------------------
  116. 取得编辑器的HTML内容。
  117. * 参数: 无
  118. * 返回: string
  119. 示例:
  120. .. sourcecode:: js
  121. var html = editor.html();
  122. html(val)
  123. --------------------------------------------------------
  124. 设置编辑器的HTML内容。
  125. * 参数:
  126. * string val: HTML
  127. * 返回: KEditor
  128. 示例:
  129. .. sourcecode:: js
  130. editor.html('<strong>HTML</strong> code');
  131. .. index:: fullHtml
  132. .. _KEditor.fullHtml:
  133. fullHtml()
  134. --------------------------------------------------------
  135. 取得完整的HTML内容,HTML包含<html>标签。
  136. * 参数: 无
  137. * 返回: string
  138. 示例:
  139. .. sourcecode:: js
  140. var fullHtml = editor.fullHtml();
  141. .. index:: text
  142. .. _KEditor.text:
  143. text()
  144. --------------------------------------------------------
  145. 取得编辑器的纯文本内容。(包含img和embed)
  146. * 参数: 无
  147. * 返回: string
  148. 示例:
  149. .. sourcecode:: js
  150. var text = editor.text();
  151. text(val)
  152. --------------------------------------------------------
  153. 设置编辑器的内容,直接显示HTML代码。
  154. * 参数:
  155. * string val: 文本
  156. * 返回: KEditor
  157. 示例:
  158. .. sourcecode:: js
  159. editor.text('<strong>HTML</strong> code');
  160. .. index:: selectedHtml
  161. .. _KEditor.selectedHtml:
  162. selectedHtml()
  163. --------------------------------------------------------
  164. 取得当前被选中的HTML内容。
  165. * 参数: 无
  166. * 返回: string
  167. 示例:
  168. .. sourcecode:: js
  169. var html = editor.selectedHtml();
  170. .. index:: count
  171. .. _KEditor.count:
  172. count([mode])
  173. --------------------------------------------------------
  174. 取得当前被选中的HTML内容。
  175. * 参数:
  176. * string mode: 可选参数,默认值为"html",mode为"html"时取得字数包含HTML代码,mode为"text"时只包含纯文本、IMG、EMBED。
  177. * 返回: Int
  178. 示例:
  179. .. sourcecode:: js
  180. htmlCount = editor.count();
  181. textCount = editor.count('text');
  182. .. index:: isEmpty
  183. .. _KEditor.isEmpty:
  184. isEmpty()
  185. --------------------------------------------------------
  186. 判断编辑器是否有可见内容,比如文本、图片、视频。
  187. * 参数: 无
  188. * 返回: Boolean
  189. 示例:
  190. .. sourcecode:: js
  191. if (editor.isEmpty()) {
  192. alert('请输入内容。');
  193. }
  194. .. index:: insertHtml
  195. .. _KEditor.insertHtml:
  196. insertHtml(val)
  197. --------------------------------------------------------
  198. 将指定的HTML内容插入到编辑区域里的光标处。
  199. * 参数:
  200. * string val: HTML
  201. * 返回: KEditor
  202. 示例:
  203. .. sourcecode:: js
  204. editor.insertHtml('<strong>HTML</strong> code');
  205. .. index:: appendHtml
  206. .. _KEditor.appendHtml:
  207. appendHtml(val)
  208. --------------------------------------------------------
  209. 将指定的HTML内容添加到编辑区域的最后位置。
  210. * 参数:
  211. * string val: HTML
  212. * 返回: KEditor
  213. 示例:
  214. .. sourcecode:: js
  215. editor.appendHtml('<strong>HTML</strong> code');
  216. .. index:: focus
  217. .. _KEditor.focus:
  218. focus()
  219. --------------------------------------------------------
  220. 编辑器聚焦。
  221. * 参数: 无
  222. * 返回: KEditor
  223. 示例:
  224. .. sourcecode:: js
  225. editor.focus();
  226. .. index:: blur
  227. .. _KEditor.blur:
  228. blur()
  229. --------------------------------------------------------
  230. 编辑器失去焦点。
  231. * 参数: 无
  232. * 返回: KEditor
  233. 示例:
  234. .. sourcecode:: js
  235. editor.blur();
  236. .. index:: sync
  237. .. _KEditor.sync:
  238. sync()
  239. --------------------------------------------------------
  240. 将编辑器的内容设置到原来的textarea控件里。
  241. * 参数: 无
  242. * 返回: KEditor
  243. 示例:
  244. .. sourcecode:: js
  245. editor.sync();
  246. .. index:: exec
  247. .. _KEditor.exec:
  248. exec(commandName)
  249. --------------------------------------------------------
  250. 执行编辑命令,替代document.execCommmand接口。
  251. * 参数:
  252. * string commandName: 命令名
  253. * 返回: KEditor
  254. 目前可用的命令:
  255. ======================= ======================= =========================================================================
  256. commandName 描述 示例
  257. ======================= ======================= =========================================================================
  258. bold 粗体 editor.exec('bold');
  259. italic 斜体 editor.exec('italic');
  260. underline 下划线 editor.exec('underline');
  261. strikethrough 删除线 editor.exec('strikethrough');
  262. forecolor 文字颜色 editor.exec('forecolor', '#333');
  263. hilitecolor 文字背景 editor.exec('hilitecolor', '#eee');
  264. fontsize 文字大小 editor.exec('fontsize', '14px');
  265. fontfamily 字体 editor.exec('fontfamily', 'SimHei');
  266. fontname 字体,fontfamily的别名 editor.exec('fontname', 'SimHei');
  267. removeformat 删除inline样式 editor.exec('removeformat');
  268. inserthtml 插入HTML editor.exec('inserthtml', '<strong>HTML</strong>');
  269. hr 插入水平线 editor.exec('hr');
  270. print 弹出打印窗口 editor.exec('print');
  271. insertimage 插入图片 editor.exec('insertimage', '1.jpg', 'title', 200, 100, 1, 'right');
  272. createlink 超级链接 editor.exec('createlink', '1.html', '_blank');
  273. unlink 取消超级链接 editor.exec('unlink');
  274. formatblock 段落 editor.exec('formatblock', '<h1>');
  275. selectall 全选 editor.exec('selectall');
  276. justifyleft 左对齐 editor.exec('justifyleft');
  277. justifycenter 居中 editor.exec('justifycenter');
  278. justifyright 右对齐 editor.exec('justifyright');
  279. justifyfull 两端对齐 editor.exec('justifyfull');
  280. insertorderedlist 编号 editor.exec('insertorderedlist');
  281. insertunorderedlist 项目符号 editor.exec('insertunorderedlist');
  282. indent 增加缩进 editor.exec('indent');
  283. outdent 减少缩进 editor.exec('outdent');
  284. subscript 下标 editor.exec('subscript');
  285. superscript 上标 editor.exec('superscript');
  286. cut 剪切 editor.exec('cut');
  287. copy 复制 editor.exec('copy');
  288. paste 粘贴 editor.exec('paste');
  289. ======================= ======================= =========================================================================
  290. .. index:: lang
  291. .. _KEditor.lang:
  292. lang(name)
  293. --------------------------------------------------------
  294. 取得语言。
  295. * 参数:
  296. * string name: language key
  297. * 返回: string
  298. 示例:
  299. .. sourcecode:: js
  300. str = editor.lang('table'); // return '表格'
  301. .. index:: loadPlugin
  302. .. _KEditor.loadPlugin:
  303. loadPlugin(name , fn)
  304. --------------------------------------------------------
  305. 动态加载插件。
  306. * 参数:
  307. * string name: 插件名
  308. * function fn: 加载成功后执行的回调函数
  309. * 返回: KEditor
  310. 示例:
  311. .. sourcecode:: js
  312. editor.loadPlugin('table', function() {
  313. alert('加载成功。');
  314. });
  315. .. index:: clickToolbar
  316. .. _KEditor.clickToolbar:
  317. clickToolbar(name)
  318. --------------------------------------------------------
  319. 执行绑定在工具栏上的点击事件函数。
  320. * 参数:
  321. * string name: item name
  322. * 返回: KEditor
  323. 示例:
  324. .. sourcecode:: js
  325. editor.clickToolbar('bold'); // 对选中文本进行加粗
  326. clickToolbar(name [, fn])
  327. --------------------------------------------------------
  328. 绑定工具栏的点击事件函数。
  329. * 参数:
  330. * string name: item name
  331. * function fn: 点击工具栏时执行的回调函数。
  332. * 返回: fn的return value
  333. 示例:
  334. .. sourcecode:: js
  335. editor.clickToolbar('bold', function() {
  336. editor.exec('bold');
  337. });
  338. .. index:: addBookmark
  339. .. _KEditor.addBookmark:
  340. addBookmark()
  341. --------------------------------------------------------
  342. 将当前数据添加到undo/redo记录里。
  343. * 参数: 无
  344. * 返回: KEditor
  345. 示例:
  346. .. sourcecode:: js
  347. editor.addBookmark();
  348. .. index:: undo
  349. .. _KEditor.undo:
  350. undo()
  351. --------------------------------------------------------
  352. 后退。
  353. * 参数: 无
  354. * 返回: KEditor
  355. 示例:
  356. .. sourcecode:: js
  357. editor.undo();
  358. .. index:: redo
  359. .. _KEditor.redo:
  360. redo()
  361. --------------------------------------------------------
  362. 撤销后退。(前进)
  363. * 参数: 无
  364. * 返回: KEditor
  365. 示例:
  366. .. sourcecode:: js
  367. editor.redo();
  368. .. index:: fullscreen
  369. .. _KEditor.fullscreen:
  370. fullscreen([bool])
  371. --------------------------------------------------------
  372. 切换全屏模式。
  373. * 参数:
  374. * Boolean bool: 默认切换(toggle)全屏模式,false时取消全屏,true时变成全屏。
  375. * 返回: KEditor
  376. 示例:
  377. .. sourcecode:: js
  378. editor.fullscreen();
  379. .. index:: readonly
  380. .. _KEditor.readonly:
  381. readonly(isReadonly)
  382. --------------------------------------------------------
  383. 设置成只读状态,或取消只读状态。
  384. * 参数:
  385. * Boolean isReadonly: false时取消只读状态,true时设置成只读状态。
  386. * 返回: KEditor
  387. 示例:
  388. .. sourcecode:: js
  389. editor.readonly(false);
  390. .. index:: createMenu
  391. .. _KEditor.createMenu:
  392. createMenu(options)
  393. --------------------------------------------------------
  394. 显示下拉菜单。
  395. * 参数:
  396. * object options: 初始化参数
  397. * 返回: KMenu ( :doc:`menu` )
  398. 示例:
  399. .. sourcecode:: js
  400. var menu = editor.createMenu({
  401. name : 'example1',
  402. width : 150
  403. });
  404. menu.addItem({
  405. title : '红色',
  406. click : function() {
  407. alert('red');
  408. }
  409. });
  410. menu.addItem({
  411. title : '白色',
  412. click : function() {
  413. alert('white');
  414. }
  415. });
  416. .. index:: hideMenu
  417. .. _KEditor.hideMenu:
  418. hideMenu()
  419. --------------------------------------------------------
  420. 隐藏下拉菜单。
  421. * 参数: 无
  422. * 返回: KEditor
  423. 示例:
  424. .. sourcecode:: js
  425. editor.hideMenu();
  426. .. index:: addContextmenu
  427. .. _KEditor.addContextmenu:
  428. addContextmenu(item)
  429. --------------------------------------------------------
  430. 添加自定义右键菜单。
  431. * 参数:
  432. * object item: 请参考 KMenu.addItem(item)的item参数
  433. * 返回: KEditor
  434. 示例:
  435. .. sourcecode:: js
  436. editor.addContextmenu({
  437. title : 'test',
  438. click : function() {
  439. alert('clicked');
  440. },
  441. cond : true,
  442. width : 150,
  443. });
  444. // 插入分割线
  445. editor.addContextmenu({ title : '-' });
  446. .. index:: hideContextmenu
  447. .. _KEditor.hideContextmenu:
  448. hideContextmenu()
  449. --------------------------------------------------------
  450. 隐藏自定义右键菜单。
  451. * 参数: 无
  452. * 返回: KEditor
  453. 示例:
  454. .. sourcecode:: js
  455. editor.hideContextmenu();
  456. .. index:: createDialog
  457. .. _KEditor.createDialog:
  458. createDialog(options)
  459. --------------------------------------------------------
  460. 显示弹出窗口(dialog)。
  461. * 参数:
  462. * object options: 初始化参数
  463. * 返回: KDialog ( :doc:`dialog` )
  464. 示例:
  465. .. sourcecode:: js
  466. var dialog = editor.createDialog({
  467. name : 'about',
  468. width : 300,
  469. title : self.lang('about'),
  470. body : '<div style="margin:20px;">Hello</div>'
  471. });
  472. .. index:: hideDialog
  473. .. _KEditor.hideDialog:
  474. hideDialog()
  475. --------------------------------------------------------
  476. 隐藏弹出窗口(dialog)。
  477. * 参数: 无
  478. * 返回: KMenu
  479. 示例:
  480. .. sourcecode:: js
  481. editor.hideDialog();