-/* post.js */
var editor = {};
var vm;
editor.run = function run(params) {
- const props = {
- item: {},
- };
-
- const editor = {
- introtext: "",
- bodytext: "",
- title: "",
- previewIntro: "",
- previewBody: "",
- previewTitle: "",
-
- showForm: true,
- showSubmit: false,
- message: "",
- action: "preview",
-
- allowedTags: {
- "a": ["href"],
- "blockquote": [],
- "i": [],
- "strong": [],
- },
- urls: {
- submission: '/api/v1/submission',
- story: '/api/v1/story',
- journal: '/api/v1/journal',
- },
- };
-
- const watch = {
- introtext: function (val, oldVal) {
- this.item.introtext = (val.length > 0) ? this.quoteHtml(val) : "";
- },
- bodytext: function (val, oldVal) {
- this.item.bodytext = (val.length > 0) ? this.quoteHtml(val) : "";
- },
- title: function (val, oldVal) {
- this.item.title = (val.length > 0) ? this.quoteTitle(val) : "";
- },
- };
-
- const methods = {
- showPreview: function (event) {
- this.action = "preview";
- this.message = "";
- const url = this.urls[this.item.type];
- this.$http.post(url, {item: this.item, action: this.$data.action}).then(
- (response) => { // success
- this.message = "";
- this.previewTitle = response.body.item.title;
- this.previewIntro = response.body.item.introtext;
- this.showForm = false;
- this.showSubmit = true;
- },
- (response) => { // fail
- if (response.body.message) {
- this.message = response.body.message;
- }
- }
- );
- },
- postSubmission: function (event) {
- this.action = "post";
- this.message = "";
- const url = this.urls[this.type];
- this.$http.post(url, {item: this.$data.item, action: this.$data.action}).then(
- (response) => { // success
- this.message = "";
- const id = response.body.id;
- const type = response.body.type;
- const url = '/' + type + '/' + id;
- this.message = "投稿しました。URL:"
- + "<a href='" + url + "'>" + url + "</a>";
- this.showForm = false;
- this.showSubmit = false;
- },
- (response) => { // fail
- if (response.body.message) {
- this.message = response.body.message;
- }
- }
- );
- },
- leavePreview: function (event) {
- this.previewTitle = "";
- this.previewIntro = "";
- this.previewBody = "";
- this.showForm = true;
- this.showSubmit = false;
- },
- quoteHtml: function (html) {
- const escaped = eh.escape(vm.allowedTags, html);
- const quoted = eh.blank_line_to_paragraph(escaped);
- const parsed = $.parseHTML(quoted, null);
- const result = [];
- parsed.forEach((i) => {
- i.normalize();
- result.push(i.outerHTML);
- });
- return result.join('\n');
- },
- quoteTitle: function (html) {
- const escaped = '<div>' + eh.escape({}, html) + '</div>';
- const parsed = $.parseHTML(escaped, null);
- const result = [];
- parsed.forEach((i) => {
- i.normalize();
- const t = i.innerHTML || i.wholeText;
- result.push(t);
- });
- return result.join('');
- },
- };
-
- Vue.component('content-editor', {
- template: '#content-editor',
- props: props,
- data: function () {return editor;},
- watch: watch,
- methods: methods,
- });
-
var data = {
item: {
title: "",
commentstatus: "enabled",
display: 1,
submissioncopy: 0,
- type: '',
+ content_type: '',
+ editing: true,
},
}
const user = params.user || {};
data.item.author = user.nickname;
- data.item.type = params.type;
+ data.item.content_type = params.type;
vm = new Vue({el: params.el,
data: data,