1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
| <template> <div class="container is-fluid"> <div> <router-link to="/posts">一覧へ</router-link>| <router-link :to="{ name: 'Post', params: { id: $route.params.id } }"> Post </router-link> </div> <div> <div class="title"> <div class="level-left"> <div class="level-item"> <p class="maintitle is-5">Title:</p> </div> <div class="level-item"> <div class="field has-addons"> <p class="control"> <input class="input" id="title-input" type="text" v-model="title" /> </p> </div> </div> </div> </div> <div> <div class="edit" id="pell-editor"></div> </div> </div> <div> <div> <button class="button" @click="save">SAVE</button> </div> </div> </div> </template>
<script> import { init, exec } from "pell"; import axios from "axios"; import qs from "qs"; import Turndown from "turndown"; import showdown from "showdown";
axios.defaults.headers.common = { "X-Requested-With": "XMLHttpRequest", "X-CSRF-TOKEN": document .querySelector('meta[name="csrf-token"]') .getAttribute("content"), };
export default { data: function () { return { editor: undefined, title: "", subText: "", interval_handler: undefined, }; }, methods: { save: async function () { const turndown = new Turndown({ headingStyle: "atx", });
turndown.addRule("u", { filter: ["u"], replacement: function (content) { return "__" + content + "__"; }, });
turndown.addRule("br", { filter: ["br"], replacement: function () { return "<br>"; }, });
const markDown = turndown.turndown(this.subText);
const save_result = await axios.patch( `/api/posts/${this.$route.params.id}`, { post: { title: this.title, maintext: markDown }, paramsSerializer: function (params) { return qs.stringify(params, { arrayFormat: "brackets" }); }, } ); console.log(`saved ${save_result.data.state}`); }, init: async function () { const result = await axios.get(`/api/posts/${this.$route.params.id}`);
this.interval_handler = setInterval(this.save, 60000);
this.editor = init({ element: document.getElementById("pell-editor"), defaultParagraphSeparator: "p", onChange: (html) => { this.subText = html; }, actions: [ "bold", { name: "Custom Underline", icon: "<b><u>U</u><b>", title: "Custom Underline", state: () => document.queryCommandState("underline"), result: () => exec("underline"), }, { name: "Custom Paragraph", icon: "<b>P<sub>ra</sub></b>", title: "Custom Paragraph", result: () => exec("formatBlock", "<p>"), }, { name: "Custom H1", icon: "<b>H<sub>1</sub></b>", title: "Custom H1", state: () => document.queryCommandState("<h1>"), result: () => { document.execCommand("formatblock", false, "<h1>"); const listId = window.getSelection().anchorNode.parentNode; listId.classList = "title is-1"; }, }, { name: "Custom H2", icon: "<b>H<sub>2</sub></b>", title: "Custom H2", state: () => document.queryCommandState("<h2>"), result: () => { document.execCommand("formatblock", false, "<h2>"); const listId = window.getSelection().anchorNode.parentNode; listId.classList = "title is-2"; }, }, { name: "Custom H3", icon: "<b>H<sub>3</sub></b>", title: "Custom H3", state: () => document.queryCommandState("<h3>"), result: () => { document.execCommand("formatblock", false, "<h3>"); const listId = window.getSelection().anchorNode.parentNode; listId.classList = "title is-3"; }, }, ], classes: { actionbar: "pell-actionbar", button: "pell-button button", content: "pell-content", selected: "pell-button-selected", }, });
this.title = result.data.title;
const classMap = { h1: "title is-1", h2: "title is-2", h3: "title is-3", };
const bindings = Object.keys(classMap).map((key) => ({ type: "output", regex: new RegExp(`<${key}(.*)>`, "g"), replace: `<${key} class="${classMap[key]}" $1>`, }));
const converter = new showdown.Converter({ extensions: [...bindings], }); converter.setOption("underline", true); converter.setOption("noHeaderId", true);
const html = converter.makeHtml(result.data.maintext); this.editor.content.innerHTML = html; this.subText = html; }, }, beforeUnmount: function () { clearInterval(this.interval_handler); }, mounted() { this.init(); }, }; </script>
<style lang="scss" scoped> .title { margin-bottom: 10px; } .edit { border: 3px solid rgba(100, 100, 100, 0.5); border-radius: 7px; }
/deep/ .pell-content { padding: 10px; }
/deep/ .pell-content:focus { outline: none; } </style>
|