new_article.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. {% load static %}
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>新文章</title>
  9. <link href="https://cdn.bootcdn.net/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet">
  10. <!-- <link href="https://cdn.jsdelivr.net/npm/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet"> -->
  11. <link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
  12. <link href="{% static 'tags-input/jquery.tagsinput.min.css' %}" rel="stylesheet">
  13. <link href="{% static 'tags-input/jquery-ui.min.css' %}" rel="stylesheet">
  14. <link href="{% static 'semantic.min.css' %}" type="text/css" rel="stylesheet">
  15. <style>
  16. .ui-autocomplete {
  17. position: absolute;
  18. z-index: 100000;
  19. cursor: default;
  20. padding: 0;
  21. margin-top: 2px;
  22. list-style: none;
  23. background-color: #ffffff;
  24. border: 1px solid #ccc;
  25. -webkit-border-radius: 5px;
  26. -moz-border-radius: 5px;
  27. border-radius: 5px;
  28. -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  29. -moz-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  30. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  31. }
  32. .ui-autocomplete > li {
  33. padding: 3px 20px;
  34. }
  35. .ui-autocomplete > li.ui-state-focus {
  36. background-color: #DDD;
  37. }
  38. .ui-helper-hidden-accessible {
  39. display: none;
  40. }
  41. html,
  42. body {
  43. background-color: #fff;
  44. height: 100%;
  45. overflow: hidden;
  46. color: #333;
  47. }
  48. #top-container {
  49. border-bottom: 1px solid #e8e8e8;
  50. padding-left: 30px;
  51. }
  52. #editor-toolbar {
  53. width: 1350px;
  54. background-color: #FCFCFC;
  55. margin: 0 auto;
  56. }
  57. #content {
  58. height: calc(100% - 40px);
  59. background-color: rgb(245, 245, 245);
  60. overflow-y: auto;
  61. position: relative;
  62. }
  63. #editor-container {
  64. width: 850px;
  65. margin: 30px auto 150px auto;
  66. background-color: #fff;
  67. padding: 20px 50px 50px 50px;
  68. border: 1px solid #e8e8e8;
  69. box-shadow: 0 2px 10px rgb(0 0 0 / 12%);
  70. }
  71. #title-container {
  72. padding: 20px 0;
  73. border-bottom: 1px solid #e8e8e8;
  74. }
  75. #title-container input {
  76. font-size: 30px;
  77. border: 0;
  78. outline: none;
  79. width: 100%;
  80. line-height: 1;
  81. }
  82. #editor-text-area {
  83. min-height: 900px;
  84. margin-top: 20px;
  85. }
  86. #tags_tag {
  87. border: 0;
  88. padding: 6px;
  89. height: 100%;
  90. }
  91. </style>
  92. </head>
  93. <body>
  94. {% csrf_token %}
  95. <div id="top-container" style="padding-left: 6px;">
  96. <p style="text-align:right;padding: 2px;">
  97. <a class="ui button secondary mini" href="/management"><i class="angle left icon"></i>返回</a>
  98. <a class="ui button youtube mini" id="restore" onclick="restore()"><i class="trash alternate icon"></i>恢复</a>
  99. <a class="ui button primary mini" onclick="submit(-1)"><i class="send icon"></i>发布</a>
  100. </p>
  101. </div>
  102. <div style="border-bottom: 1px solid #e8e8e8;">
  103. <div id="editor-toolbar"></div>
  104. </div>
  105. <div id="content">
  106. <div id="editor-container">
  107. <div id="title-container">
  108. <input id="page_title" placeholder="Page title...">
  109. </div>
  110. <div id="editor-text-area"></div>
  111. </div>
  112. </div>
  113. <div class="ui longer modal">
  114. <div class="header">
  115. 额外信息
  116. </div>
  117. <div class="scrolling content">
  118. <div class="description">
  119. <form onsubmit="return false" id="article_form" method="post" class="ui form">
  120. {% csrf_token %}
  121. <div class="field">
  122. <label>标题</label>
  123. <div class="field">
  124. <input readonly type="text" id="title" name="title" placeholder="标题">
  125. </div>
  126. <div class="field">
  127. <label>摘要</label>
  128. <textarea type="text" name="intro"
  129. placeholder="摘要">{{ article.intro }}</textarea>
  130. </div>
  131. </div>
  132. <div class="field">
  133. <div class="fields">
  134. <div class="field">
  135. <label>栏目</label>
  136. <select name="category" class="ui dropdown">
  137. {% for item in category %}
  138. <option value="{{ item.id }}">{{ item.name }}</option>
  139. {% endfor %}
  140. </select>
  141. </div>
  142. <div class="field">
  143. <label>置顶</label>
  144. <select name="is_top" class="ui dropdown">
  145. <option value="1">YES</option>
  146. <option value="0">NO</option>
  147. </select>
  148. </div>
  149. </div>
  150. </div>
  151. <div class="field">
  152. <label>标签</label>
  153. <div class="fields">
  154. <div class="sixteen wide field">
  155. <input name="tags" id="tags" value=""/>
  156. </div>
  157. </div>
  158. </div>
  159. </form>
  160. </div>
  161. </div>
  162. <div class="actions">
  163. <div onclick="submit(1)" class="ui primary approve button">
  164. 发布
  165. </div>
  166. <div onclick="submit(0)" class="ui secondary approve button">
  167. 保存
  168. </div>
  169. </div>
  170. </div>
  171. <!-- 显示内容 -->
  172. <script src="{% static 'js/jquery.min.js' %}"></script>
  173. <script src="{% static 'semantic.js' %}"></script>
  174. <script src="{% static 'tags-input/jquery-ui.min.js' %}"></script>
  175. <script src="{% static 'tags-input/jquery.tagsinput.min.js' %}"></script>
  176. <!-- <script src="https://cdn.jsdelivr.net/npm/@wangeditor/editor@latest/dist/index.min.js"></script> -->
  177. <script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
  178. <script>
  179. const E = window.wangEditor
  180. // 切换语言
  181. const LANG = location.href.indexOf('lang=en') > 0 ? 'en' : 'zh-CN'
  182. E.i18nChangeLanguage(LANG)
  183. const imageToHtmlConf = {
  184. type: 'image',
  185. elemToHtml: function imageToHtml(elemNode) {
  186. const {src, alt, href = '', style = {}, width, height} = elemNode || {}
  187. if (alt === undefined || alt === null || alt === '') {
  188. return `<img src="${src}" alt="${alt}">`
  189. } else {
  190. return `<img alt="${alt}" src="${src}"/>`
  191. }
  192. }
  193. };
  194. const editorConfig = {
  195. placeholder: 'Type here...',
  196. scroll: false, // 禁止编辑器滚动
  197. MENU_CONF: {
  198. fontFamily: {
  199. fontFamilyList: [
  200. {
  201. name: '宋体',
  202. value: 'SimSun'
  203. }, {
  204. name: '黑体',
  205. value: 'SimHei'
  206. }, {
  207. name: '微软雅黑',
  208. value: 'Microsoft Yahei'
  209. }, {
  210. name: '微软正黑体',
  211. value: 'Microsoft JhengHei'
  212. }, {
  213. name: '楷体',
  214. value: 'KaiTi'
  215. }, {
  216. name: '华文仿宋',
  217. value: 'STFangsong'
  218. }, {
  219. name: '新宋体',
  220. value: 'NSimSun'
  221. }, {
  222. name: '仿宋',
  223. value: 'FangSong'
  224. }, {
  225. name: '苹方',
  226. value: 'PingFang SC'
  227. }, {
  228. name: '华文黑体',
  229. value: 'STHeiti'
  230. }, {
  231. name: '华文楷体',
  232. value: 'STKaiti'
  233. }, {
  234. name: '华文细黑',
  235. value: 'STXihei'
  236. }, {
  237. name: '翩翩体-简',
  238. value: 'Hanzipen SC'
  239. },
  240. 'Arial',
  241. 'Tahoma',
  242. 'Verdana',
  243. 'Noto Serif SC'
  244. ]
  245. },
  246. uploadImage: {
  247. server: '/management/files/upload',
  248. // form-data fieldName ,默认值 'wangeditor-uploaded-image'
  249. fieldName: 'file',
  250. // 单个文件的最大体积限制,默认为 2M
  251. maxFileSize: 4 * 1024 * 1024, // 1M
  252. // 最多可上传几个文件,默认为 100
  253. maxNumberOfFiles: 10,
  254. // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
  255. allowedFileTypes: ['image/*'],
  256. // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
  257. meta: {
  258. csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val()
  259. },
  260. // 将 meta 拼接到 url 参数中,默认 false
  261. metaWithUrl: false,
  262. // 自定义增加 http header
  263. // 跨域是否传递 cookie ,默认为 false
  264. withCredentials: true,
  265. // 超时时间,默认为 10 秒
  266. timeout: 5 * 1000, // 5 秒
  267. }
  268. },
  269. onChange(editor) {
  270. const html = editor.getHtml()
  271. let title = $('#page_title').val()
  272. if (title === '' || title === undefined) {
  273. title = '未命名标题'
  274. }
  275. if (html.length > 50) {
  276. localStorage.setItem('article_buffer', JSON.stringify({
  277. 'title': title,
  278. 'html': html
  279. }))
  280. $('#title').val($('#page_title').val())
  281. }
  282. }
  283. }
  284. E.Boot.registerElemToHtml(imageToHtmlConf);
  285. // 先创建 editor
  286. const editor = E.createEditor({
  287. selector: '#editor-text-area',
  288. content: [],
  289. // html: '',
  290. config: editorConfig
  291. })
  292. // 创建 toolbar
  293. const toolbar = E.createToolbar({
  294. editor,
  295. selector: '#editor-toolbar',
  296. config: {
  297. excludeKeys: 'fullScreen',
  298. }
  299. })
  300. // 点击空白处 focus 编辑器
  301. document.getElementById('editor-text-area').addEventListener('click', e => {
  302. if (e.target.id === 'editor-text-area') {
  303. editor.blur()
  304. editor.focus(true) // focus 到末尾
  305. }
  306. })
  307. function restore() {
  308. if (localStorage.getItem('article_buffer') !== null) {
  309. let item = JSON.parse(localStorage.getItem('article_buffer'))
  310. $('#page_title').val(item.title)
  311. editor.dangerouslyInsertHtml(item.html)
  312. }
  313. }
  314. </script>
  315. <script>
  316. function submit(flag) {
  317. $(".ui.form").form({
  318. fields: {
  319. title: {
  320. identifier: "title",
  321. rules: [
  322. {
  323. type: "empty",
  324. prompt: "请输入标题"
  325. }
  326. ]
  327. },
  328. intro: {
  329. identifier: "intro",
  330. rules: [
  331. {
  332. type: "empty",
  333. prompt: "请输入摘要"
  334. }
  335. ]
  336. }
  337. },
  338. inline: true,
  339. on: "blur"
  340. });
  341. $('#title').val($('#page_title').val())
  342. if (flag === -1) {
  343. $('.ui.longer.modal')
  344. .modal('show')
  345. } else {
  346. let isPass = true
  347. var data = $('#article_form').serializeArray();
  348. data.forEach((item, i) => {
  349. if (item.value === '') {
  350. isPass = false
  351. }
  352. })
  353. if (isPass) {
  354. let formData = new FormData()
  355. data.forEach((item, i) => {
  356. formData.append(item.name, item.value)
  357. })
  358. formData.append('html', editor.getHtml())
  359. formData.append('markdown', editor.getText())
  360. formData.append('type', 1)
  361. formData.append('tags', $('#tags').val())
  362. formData.append('status', flag)
  363. $.ajax('/management/article/add_article', {
  364. method: 'post',
  365. data: formData,
  366. contentType: false,
  367. processData: false,
  368. success: function (res) {
  369. window.location.href = '/management/article'
  370. },
  371. error: function (err) {
  372. }
  373. })
  374. } else {
  375. alert('请检查表单')
  376. }
  377. }
  378. }
  379. $('#tags').tagsInput({
  380. 'autocomplete_url': '/management/tags/search',
  381. 'autocomplete': {selectFirst: true, width: '100px', autoFill: true},
  382. 'height': '100px',
  383. 'width': '100%',
  384. 'interactive': true,
  385. 'defaultText': '添加标签',
  386. 'delimiter': [','], // Or a string with a single delimiter. Ex: ';'
  387. 'removeWithBackspace': true,
  388. 'minChars': 0,
  389. 'maxChars': 0, // if not provided there is no limit
  390. 'placeholderColor': '#666666',
  391. });
  392. $(".ui.toggle.button").click(function () {
  393. $(".mobile.only.grid .ui.vertical.menu").toggle(100);
  394. });
  395. $(".ui.dropdown").dropdown();
  396. function generateIntro() {
  397. let text = editor.getText()
  398. if (text.length > 20) {
  399. $("input[name='intro']").val(text.substring(0, 20) + '...')
  400. } else {
  401. $("input[name='intro']").val(text + '...')
  402. }
  403. }
  404. </script>
  405. </body>
  406. </html>