123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- var faCogActive = true;
- var faThLargeActive = false;
- var activeIcon = getVariable('active_icon');
- if (activeIcon) {
- faCogActive = activeIcon === 'fa-cog'? true : false;
- faThLargeActive = activeIcon === 'fa-th-large'? true : false;
- }
- const panelsConfig = {
- stylePrefix: 'pn-',
- defaults: [
- {
- id: 'commands',
- buttons: [{}],
- },
- {
- id: 'options',
- buttons: [
- {
- active: false,
- id: 'sw-visibility',
- className: 'fa fa-square-o',
- command: 'core:component-outline',
- context: 'sw-visibility',
- attributes: { title: 'View components' },
- },
- {
- id: 'preview',
- className: 'fa fa-eye',
- command: 'preview',
- context: 'preview',
- attributes: { title: 'Preview' },
- },
- {
- id: 'fullscreen',
- className: 'fa fa-arrows-alt',
- command: 'fullscreen',
- context: 'fullscreen',
- attributes: { title: 'Fullscreen' },
- },
-
-
-
-
-
-
- ],
- },
- {
- id: 'views',
- buttons: [
-
-
-
-
-
-
-
-
- {
- id: 'open-tm',
- className: 'fa fa-cog',
- command: 'open-tm',
- active: faCogActive,
- togglable: true,
- attributes: { title: 'Settings' },
- },
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ],
- },
- ],
- };
- const myNewComponentTypes = (editor) => {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- };
- const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
- const editor = grapesjs.init({
- container: '#mtb_visual_editor',
- fromElement: true,
- height: '100%',
- width: '100%',
- storageManager: false,
- panels: panelsConfig,
- plugins: [myNewComponentTypes],
- assetManager: {
- upload: '/dist/visual-editor/upload',
- uploadName: '_file_',
- multiUpload:false,
- headers: {
- 'X-CSRF-TOKEN': csrfToken,
- },
- },
- });
- const bm = editor.Blocks;
- bm.add('block_p', {
-
- label: 'Paragraph',
- media: `<svg style="width:20px;height:20px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M192 32l64 0 160 0c17.7 0 32 14.3 32 32s-14.3 32-32 32l-32 0 0 352c0 17.7-14.3 32-32 32s-32-14.3-32-32l0-352-32 0 0 352c0 17.7-14.3 32-32 32s-32-14.3-32-32l0-96-32 0c-88.4 0-160-71.6-160-160s71.6-160 160-160z"/></svg>`,
- content: '<p>insert your paragraph here</p>',
- });
- bm.add('block_span', {
-
- label: 'Text',
- media: `<svg style="width:20px;height:20px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M32 32C14.3 32 0 46.3 0 64S14.3 96 32 96l128 0 0 352c0 17.7 14.3 32 32 32s32-14.3 32-32l0-352 128 0c17.7 0 32-14.3 32-32s-14.3-32-32-32L192 32 32 32z"/></svg>`,
- content: '<span>insert your text here</span>',
- });
- const am = editor.AssetManager;
- editor.getWrapper().set({ locked: true });
- editor.getComponents().each(component => {
- component.onAll(component => {
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (component.get('attributes').mtb_edit == '1' || component.get('attributes').mtb_edit == 'true') {
- component.set({ locked: false });
- setBorder(component);
- }
- if (component.get('attributes').mtb_edit == '0' || component.get('attributes').mtb_edit == 'false') {
- component.set({ locked: true });
- }
- if (component.get('attributes').mtb_edit == '2') {
- component.set({ locked: false });
- setBorder(component);
-
- component.forEachChild(child => {
- child.set({ locked: true });
- })
- }
- })
- });
- function setBorder(component) {
- if (component.attributes.tagName == 'a') {
- component.setStyle({
- border: '2px dotted #f08300'
- });
- } else {
- component.setStyle({
- border: '1px dotted #3b97e3'
- });
- }
- }
- editor.on('component:selected', (comp) => {
-
- const selectedComponent = editor.getSelected();
-
-
- if (selectedComponent.get('attributes').mtb_edit == 'true' || selectedComponent.get('attributes').mtb_edit == '1' || selectedComponent.get('attributes').mtb_edit == '2') {
- selectedComponent.set({
- toolbar: [
- ]
- });
- } else {
- selectedComponent.set({
- toolbar: [
- ]
-
-
-
-
-
-
-
-
-
-
- });
- }
- if (selectedComponent.get('attributes').mtb_toolbar != '' && selectedComponent.get('attributes').mtb_toolbar != undefined) {
- const mtbToolbar = selectedComponent.get('attributes').mtb_toolbar;
- const safeActions = mtbToolbar.split(',');
- console.log(safeActions);
- let inputToolbar = [];
- if (safeActions.includes('clone')) {
-
- inputToolbar.push({
- attributes: { class: 'fa fa-clone' },
- command: 'tlb-clone',
- title: 'Clone',
- });
- }
- if (safeActions.includes('delete')) {
-
- inputToolbar.push({
- attributes: { class: 'fa fa-trash' },
- command: 'tlb-delete',
- title: 'Delete',
- });
- }
- selectedComponent.set({
- toolbar: inputToolbar
- });
- }
-
- if (selectedComponent.attributes.tagName == 'img') {
-
- const el = selectedComponent.getEl();
-
- el.addEventListener('dblclick', () => {
- am.open({
- types: ['image'],
- select(asset, complete) {
- const selected = editor.getSelected();
- if (selected) {
- selected.addAttributes({ src: asset.getSrc() });
- complete && am.close();
- }
- },
- });
- });
- }
-
- });
- function leftPanelDisplay(isShow = true) {
- if (isShow) {
- let elements1 = document.querySelectorAll('.gjs-pn-views-container');
- elements1.forEach(function(element) {
- element.style.display = 'block';
- });
- let elements2 = document.querySelectorAll('.gjs-pn-views');
- elements2.forEach(function(element) {
- element.style = 'border_bottom: 2px solid var(--gjs-main-dark-color)';
- });
- } else {
- let elements1 = document.querySelectorAll('.gjs-pn-views-container');
- elements1.forEach(function(element) {
- element.style.display = 'none';
- });
- let elements2 = document.querySelectorAll('.gjs-pn-views');
- elements2.forEach(function(element) {
- element.style = 'border-bottom:none';
- });
- }
- }
- editor.on('load', () => {
-
-
-
-
- const targetElement = document.querySelector('.fa-arrows-alt');
-
- const dropdown = document.createElement('select');
- dropdown.className = 'custom-select';
- dropdown.id = 'dropdown';
-
- var options = '';
- siteMenu.forEach(function (item) {
- if (mid == item.id) {
- options += `<option value="${item.id}" uri="${item.url}" selected>${item.title}</option>`;
- } else {
- options += `<option value="${item.id}" uri="${item.url}">${item.title}</option>`;
- }
- });
-
- dropdown.innerHTML = options;
-
- targetElement.insertAdjacentElement('afterend', dropdown);
- dropdownel = document.getElementById('dropdown');
-
- dropdownel.addEventListener('change', function () {
-
- var selectedOption = dropdownel.options[dropdownel.selectedIndex];
-
- var id = selectedOption.value;
- var uri = selectedOption.getAttribute('uri');
- if (id > 0) {
-
- var redirectUrl = `?mid=${id}&uri=${encodeURIComponent(uri)}`;
-
- window.location.href = redirectUrl;
- }
- });
-
-
-
- customSelect = document.querySelector('.custom-select');
-
- const sendButton = document.createElement('button');
- sendButton.textContent = publishBtnName;
- sendButton.classList.add('send-button');
- sendButton.classList.add('layui-btn');
- sendButton.classList.add('layui-btn-radius');
- sendButton.classList.add('layui-btn-radius');
- sendButton.style = 'margin-left:10px;height:25px;line-height:25px;padding:0 10px;vertical-align:unset;';
- customSelect.insertAdjacentElement('afterend', sendButton);
-
- sendButton.addEventListener('click', function () {
-
- loadIndex = layer.load(0, {shade: [0.5, '#000']});
-
- $.ajax({
- url: '/dist/visual-editor/publish',
- type: 'POST',
- headers: {
- 'X-CSRF-TOKEN': csrfToken,
- },
- data: {},
- success: function (res) {
- layer.close(loadIndex);
- layer.msg(res.message,{shade: [0.5, '#000']});
- },
- error: function (err) {
- layer.close(loadIndex);
- layer.msg(err,{shade: [0.5, '#000']});
- }
- });
- });
-
-
-
-
- var gjsPnBtn = document.querySelectorAll('.gjs-pn-btn');
- var headRightBtn = ['fa-cog', 'fa-th-large'];
- gjsPnBtn.forEach(function(icon1) {
- let hasIntersection = headRightBtn.some(function(btnClass) {
- return icon1.classList.contains(btnClass);
- });
- if (hasIntersection) {
- icon1.addEventListener('click', function() {
- hasActive = false;
- gjsPnBtn.forEach(function(icon) {
- if (icon.classList.contains('gjs-pn-active')) {
- hasActive = true;
- saveVariable('active_icon',icon.classList.item(2));
- }
- });
- if (hasActive) {
- leftPanelDisplay(true);
- } else {
- leftPanelDisplay(false);
- saveVariable('active_icon','none');
- }
- });
- }
- });
-
- if (faCogActive === false && faThLargeActive === false) {
- leftPanelDisplay(false)
- }
-
-
-
- layer.close(loadIndex);
- $('body').css({
- visibility: 'visible',
- opacity: 1
- });
- });
- editor.on('update', () => {
- const html = editor.getHtml();
-
-
- $.ajax({
- url: '/dist/visual-editor/preview-save',
- type: 'POST',
- headers: {
- 'X-CSRF-TOKEN': csrfToken,
- },
- data: {
- html: html
- },
- success: function (res) {
- if (res.status != 1) {
- layer.msg('save data error, please try again later!',{shade: [0.5, '#000']});
- }
- },
- error: function (err) {
- alert(err);
- }
- });
- });
- editor.on('asset:upload:start', () => {
- loadIndex = layer.load(0, {shade: [0.5, '#000']});
- });
- editor.on('asset:upload:end', () => {
- layer.close(loadIndex);
- });
- layui.use('flow', function(){
- var flow = layui.flow;
-
- flow.lazyimg();
- });
- function saveVariable(name, value) {
- localStorage.setItem(name, JSON.stringify(value));
- }
- function getVariable(name) {
- const value = localStorage.getItem(name);
- return value ? JSON.parse(value) : null;
- }
|