/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ function saveEdits() { var components = {}; $(".sw-editable").each(function (e) { components[$(this).data("component")] = tinymce.get($(this).attr("id")).getContent(); }); $(".sw-text-input").each(function (e) { components[$(this).data("component")] = $(this).val(); }); $(".sw-complex").each(function (e) { if (typeof $(this).data("json") === "string") { components[$(this).data("component")] = JSON.parse($(this).data("json")); } else { components[$(this).data("component")] = $(this).data("json"); } }); var output = { slug: page_slug, site: site_id, content: components }; //console.log(output); var json = JSON.stringify(output); console.log(output); console.log("editor: sent page content"); parent.postMessage('save ' + json, "*"); } $(document).ready(function () { $('a').click(function (e) { e.preventDefault(); }); $("body").append(""); $(".sw-editable").each(function () { // Remove leading whitespace added by the template $(this).html($(this).html().trim()); }); tinymce.init({ selector: '.sw-editable', inline: true, paste_data_images: true, plugins: [ 'autolink lists link image imagetools charmap', 'searchreplace visualblocks code fullscreen', 'media table contextmenu paste code' ], branding: false, /*menu: { edit: {title: 'Edit', items: 'undo redo | cut copy paste pastetext | selectall | findreplace'}, view: {title: 'View', items: 'sourcecode | visualaid showblocks'}, insert: {title: 'Insert', items: 'image link media table | charmap'}, format: {title: 'Format', items: 'bold italic underline strikethrough superscript subscript | blocks align formats | removeformat'}, table: {title: 'Table', items: 'inserttable tableprops deletetable | cell row column'} },*/ menubar: 'edit insert view format table tools', toolbar: 'insert | undo redo | formatselect | bold italic | bullist numlist outdent indent | removeformat | fullscreen', link_list: function (success) { success(pages_list); }, mobile: { theme: 'mobile' } }); $(".sw-text").each(function () { var text = $(this).text().trim(); var component = $(this).data("component"); $(this).html(""); }); $(".sw-complex").each(function () { var eid = $(this).attr("id"); var eclass = $(this).attr("class"); var estyle = $(this).attr("style"); var ecomp = $(this).data("component"); var ejson = $(this).data("json"); if (typeof ejson !== "string") { ejson = JSON.stringify(ejson); } $(this).replaceWith( $('
', { id: eid, class: eclass, style: estyle, html: $(this).html(), "data-component": ecomp, "data-json": ejson })); $("[data-component=\"" + ecomp + "\"]").append("
Click to edit
"); }); $(".sw-editbtn").on("click", function () { var data = $(this).parent().data("json"); var send = {"component": $(this).parent().data("component"), "content": data}; //console.log(send); parent.postMessage('editcomplex ' + JSON.stringify(send), "*"); return false; }); window.addEventListener('message', function (event) { console.log("editor: received message: " + event.data); if (event.data == "save") { saveEdits(); } else if (event.data.startsWith("complex ")) { var json = JSON.parse(event.data.slice(8)); var comp = json["component"]; var data = json["content"]; $(".sw-complex[data-component='" + comp + "']").data("json", JSON.stringify(data)); } }); });