new_article.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 primary mini" onclick="submit(-1)"><i class="send icon"></i>发布</a>
  99. </p>
  100. </div>
  101. <div style="border-bottom: 1px solid #e8e8e8;">
  102. <div id="editor-toolbar"></div>
  103. </div>
  104. <div id="content">
  105. <div id="editor-container">
  106. <div id="title-container">
  107. <input id="page_title" placeholder="Page title...">
  108. </div>
  109. <div id="editor-text-area"></div>
  110. </div>
  111. </div>
  112. <div class="ui longer modal">
  113. <div class="header">
  114. 额外信息
  115. </div>
  116. <div class="scrolling content">
  117. <div class="description">
  118. <form id="article_form" method="post" class="ui form">
  119. {% csrf_token %}
  120. <div class="field">
  121. <label>标题</label>
  122. <div class="field">
  123. <input readonly type="text" id="title" name="title" placeholder="标题">
  124. </div>
  125. <div class="field">
  126. <label>摘要</label>
  127. <textarea type="text" name="intro"
  128. placeholder="摘要">{{ article.intro }}</textarea>
  129. </div>
  130. </div>
  131. <div class="field">
  132. <div class="fields">
  133. <div class="field">
  134. <label>栏目</label>
  135. <select name="category" class="ui dropdown">
  136. {% for item in category %}
  137. <option value="{{ item.id }}">{{ item.name }}</option>
  138. {% endfor %}
  139. </select>
  140. </div>
  141. <div class="field">
  142. <label>置顶</label>
  143. <select name="is_top" class="ui dropdown">
  144. <option value="1">YES</option>
  145. <option value="0">NO</option>
  146. </select>
  147. </div>
  148. </div>
  149. </div>
  150. <div class="field">
  151. <label>标签</label>
  152. <div class="fields">
  153. <div class="sixteen wide field">
  154. <input name="tags" id="tags" value=""/>
  155. </div>
  156. </div>
  157. </div>
  158. </form>
  159. </div>
  160. </div>
  161. <div class="actions">
  162. <div onclick="submit(1)" class="ui primary approve button">
  163. 发布
  164. </div>
  165. <div onclick="submit(2)" class="ui secondary approve button">
  166. 保存
  167. </div>
  168. </div>
  169. </div>
  170. <!-- 显示内容 -->
  171. <script src="{% static 'js/jquery.min.js' %}"></script>
  172. <script src="{% static 'semantic.js' %}"></script>
  173. <script src="{% static 'tags-input/jquery-ui.min.js' %}"></script>
  174. <script src="{% static 'tags-input/jquery.tagsinput.min.js' %}"></script>
  175. <!-- <script src="https://cdn.jsdelivr.net/npm/@wangeditor/editor@latest/dist/index.min.js"></script> -->
  176. <script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
  177. <script>
  178. $(document).on("keydown", "form", function (event) {
  179. return event.key != "Enter";
  180. });
  181. const E = window.wangEditor
  182. // 切换语言
  183. const LANG = location.href.indexOf('lang=en') > 0 ? 'en' : 'zh-CN'
  184. E.i18nChangeLanguage(LANG)
  185. const imageToHtmlConf = {
  186. type: 'image',
  187. elemToHtml: function imageToHtml(elemNode) {
  188. const {src, alt, href = '', style = {}, width, height} = elemNode || {}
  189. return `<a data-pswp-width="${alt.split(',')[1]}" data-pswp-height="${alt.split(',')[1]}" href="${src}"><img data-magnify="gallery" data-src="${src}" src="${src}" data-href="${href}" "/></a>`
  190. }
  191. };
  192. const editorConfig = {
  193. placeholder: 'Type here...',
  194. scroll: false, // 禁止编辑器滚动
  195. MENU_CONF: {
  196. uploadImage: {
  197. server: '/management/files/upload',
  198. // form-data fieldName ,默认值 'wangeditor-uploaded-image'
  199. fieldName: 'file',
  200. // 单个文件的最大体积限制,默认为 2M
  201. maxFileSize: 4 * 1024 * 1024, // 1M
  202. // 最多可上传几个文件,默认为 100
  203. maxNumberOfFiles: 10,
  204. // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
  205. allowedFileTypes: ['image/*'],
  206. // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
  207. meta: {
  208. csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val()
  209. },
  210. // 将 meta 拼接到 url 参数中,默认 false
  211. metaWithUrl: false,
  212. // 自定义增加 http header
  213. // 跨域是否传递 cookie ,默认为 false
  214. withCredentials: true,
  215. // 超时时间,默认为 10 秒
  216. timeout: 5 * 1000, // 5 秒
  217. }
  218. },
  219. onChange(editor) {
  220. const html = editor.getHtml()
  221. $('#title').val($('#page_title').val())
  222. }
  223. }
  224. E.Boot.registerElemToHtml(imageToHtmlConf);
  225. // 先创建 editor
  226. const editor = E.createEditor({
  227. selector: '#editor-text-area',
  228. content: [],
  229. // html: '',
  230. config: editorConfig
  231. })
  232. // 创建 toolbar
  233. const toolbar = E.createToolbar({
  234. editor,
  235. selector: '#editor-toolbar',
  236. config: {
  237. excludeKeys: 'fullScreen',
  238. }
  239. })
  240. // 点击空白处 focus 编辑器
  241. document.getElementById('editor-text-area').addEventListener('click', e => {
  242. if (e.target.id === 'editor-text-area') {
  243. editor.blur()
  244. editor.focus(true) // focus 到末尾
  245. }
  246. })
  247. </script>
  248. <script>
  249. function submit(flag) {
  250. $(".ui.form").form({
  251. fields: {
  252. title: {
  253. identifier: "title",
  254. rules: [
  255. {
  256. type: "empty",
  257. prompt: "请输入标题"
  258. }
  259. ]
  260. },
  261. intro: {
  262. identifier: "intro",
  263. rules: [
  264. {
  265. type: "empty",
  266. prompt: "请输入摘要"
  267. }
  268. ]
  269. }
  270. },
  271. inline: true,
  272. on: "blur"
  273. });
  274. $('#title').val($('#page_title').val())
  275. if (flag === -1) {
  276. $('.ui.longer.modal')
  277. .modal('show')
  278. } else {
  279. let isPass = true
  280. var data = $('#article_form').serializeArray();
  281. data.forEach((item, i) => {
  282. if (item.value === '') {
  283. isPass = false
  284. }
  285. })
  286. if (isPass) {
  287. let formData = new FormData()
  288. data.forEach((item, i) => {
  289. formData.append(item.name, item.value)
  290. })
  291. formData.append('html', editor.getHtml())
  292. formData.append('markdown', editor.getText())
  293. formData.append('type', 1)
  294. formData.append('tags', $('#tags').val())
  295. formData.append('status', flag)
  296. $.ajax('/management/article/add_article', {
  297. method: 'post',
  298. data: formData,
  299. contentType: false,
  300. processData: false,
  301. success: function (res) {
  302. window.location.href = '/management/article'
  303. },
  304. error: function (err) {
  305. }
  306. })
  307. } else {
  308. alert('请检查表单')
  309. }
  310. }
  311. }
  312. $('#tags').tagsInput({
  313. 'autocomplete_url': '/management/tags/search',
  314. 'autocomplete': {selectFirst: true, width: '100px', autoFill: true},
  315. 'height': '100px',
  316. 'width': '100%',
  317. 'interactive': true,
  318. 'defaultText': '添加标签',
  319. 'delimiter': [','], // Or a string with a single delimiter. Ex: ';'
  320. 'removeWithBackspace': true,
  321. 'minChars': 0,
  322. 'maxChars': 0, // if not provided there is no limit
  323. 'placeholderColor': '#666666',
  324. });
  325. $(".ui.toggle.button").click(function () {
  326. $(".mobile.only.grid .ui.vertical.menu").toggle(100);
  327. });
  328. $(".ui.dropdown").dropdown();
  329. function generateIntro() {
  330. let text = editor.getText()
  331. if (text.length > 20) {
  332. $("input[name='intro']").val(text.substring(0, 20) + '...')
  333. } else {
  334. $("input[name='intro']").val(text + '...')
  335. }
  336. }
  337. </script>
  338. </body>
  339. </html>