Commit realizado el 12:13:52 08-04-2024
This commit is contained in:
103
wp-content/themes/Divi/core/code-snippets/app/boot.js
Normal file
103
wp-content/themes/Divi/core/code-snippets/app/boot.js
Normal file
@@ -0,0 +1,103 @@
|
||||
// External Dependencies
|
||||
import React from 'react';
|
||||
import { render, unmountComponentAtNode } from 'react-dom';
|
||||
import $ from 'jquery';
|
||||
import App from 'cerebral';
|
||||
import Devtools from 'cerebral/devtools';
|
||||
import { Container } from '@cerebral/react';
|
||||
import {
|
||||
assign,
|
||||
clone,
|
||||
set,
|
||||
get,
|
||||
} from 'lodash';
|
||||
|
||||
// Internal Dependencies
|
||||
import store from './store/';
|
||||
import CodeSnippetsApp from './components/App';
|
||||
import { STATE_IDLE } from '@code-snippets/lib/constants';
|
||||
|
||||
|
||||
const initialState = {
|
||||
content: '',
|
||||
context: 'code_css',
|
||||
importError: false,
|
||||
items: [],
|
||||
showLibrary: false,
|
||||
showPortability: false,
|
||||
importState: STATE_IDLE,
|
||||
};
|
||||
|
||||
$(window).on('et_code_snippets_container_ready', (event, preferences, container = document) => {
|
||||
let devtools = null;
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
devtools = Devtools({
|
||||
host: '127.0.0.1:4045',
|
||||
reconnect: false,
|
||||
bigComponentsWarning: 15,
|
||||
});
|
||||
}
|
||||
|
||||
const context = preferences.context;
|
||||
const state = assign(clone(initialState), {});
|
||||
const modalType = preferences.modalType;
|
||||
|
||||
if ('' !== context) {
|
||||
set(state, 'context', context);
|
||||
}
|
||||
|
||||
const content = get(preferences, 'content', '');
|
||||
if ('' !== content) {
|
||||
set(state, 'content', content);
|
||||
}
|
||||
|
||||
const selectedContent = get(preferences, 'selectedContent', '');
|
||||
set(state, 'selectedContent', selectedContent);
|
||||
|
||||
const sidebarLabel = get(preferences, 'sidebarLabel', '');
|
||||
set(state, 'sidebarLabel', sidebarLabel);
|
||||
|
||||
const app = App(store(state), {
|
||||
devtools,
|
||||
returnSequencePromise: true,
|
||||
});
|
||||
|
||||
const containerId = preferences.containerId;
|
||||
|
||||
const domNode = container.getElementById(containerId);
|
||||
if ('' === modalType) {
|
||||
unmountComponentAtNode(domNode);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
render(
|
||||
<Container app={app}>
|
||||
<CodeSnippetsApp
|
||||
modalType={modalType}
|
||||
codeMirrorId={preferences.codeMirrorId}
|
||||
insertCodeCallback={preferences.insertCodeCallback}
|
||||
container={container}
|
||||
/>
|
||||
</Container>,
|
||||
domNode
|
||||
);
|
||||
|
||||
// Disable main body scrolling.
|
||||
$(container).find('body.et-admin-page').addClass('et-code-snippets-open');
|
||||
|
||||
// Properly unmount app on close.
|
||||
$(window).on('et_code_snippets_library_close', () => {
|
||||
const appContainer = container.getElementById(containerId);
|
||||
|
||||
setTimeout(() => {
|
||||
if (appContainer) {
|
||||
unmountComponentAtNode(appContainer);
|
||||
appContainer.remove();
|
||||
}
|
||||
|
||||
$('body.et-admin-page').removeClass('et-code-snippets-open');
|
||||
});
|
||||
});
|
||||
});
|
@@ -0,0 +1,27 @@
|
||||
// External dependencies.
|
||||
import {
|
||||
every,
|
||||
get,
|
||||
isArray,
|
||||
isEmpty,
|
||||
} from 'lodash';
|
||||
|
||||
// Internal dependencies.
|
||||
import config from './config';
|
||||
|
||||
|
||||
const isAllowedActionPure = (capabilities, action, restrictByDefault = false) => {
|
||||
if (isEmpty(action)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const defaultValue = restrictByDefault ? 'off' : 'on';
|
||||
|
||||
if (isArray(action)) {
|
||||
return every(action, action => 'on' === get(capabilities, action, defaultValue));
|
||||
}
|
||||
|
||||
return 'on' === get(capabilities, action, defaultValue);
|
||||
};
|
||||
|
||||
export const isAllowedAction = (...args) => isAllowedActionPure(config.capabilities, ...args);
|
@@ -0,0 +1,32 @@
|
||||
// Set global variable to detect new library item creation.
|
||||
window.CodeSnippetItemsLoaded = {};
|
||||
|
||||
export const setCodeSnippetItemsLoaded = (context, flag) => {
|
||||
window.CodeSnippetItemsLoaded = {
|
||||
...CodeSnippetItemsLoaded,
|
||||
[context] : flag,
|
||||
};
|
||||
};
|
||||
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
export const getItemTypeByContext = context => {
|
||||
let type;
|
||||
|
||||
switch (context) {
|
||||
case 'code_html':
|
||||
type = 'et_code_snippet_html_js';
|
||||
break;
|
||||
case 'code_css_no_selector':
|
||||
type = 'et_code_snippet_css_no_selector';
|
||||
break;
|
||||
default:
|
||||
type = 'et_code_snippet_css';
|
||||
break;
|
||||
}
|
||||
|
||||
return type;
|
||||
};
|
||||
|
||||
export const setCodeSnippetsLibraryToken = (token) => {
|
||||
window.globalCloudToken = token;
|
||||
};
|
@@ -0,0 +1 @@
|
||||
export default window.et_code_snippets_data;
|
@@ -0,0 +1,10 @@
|
||||
export const MODAL_TYPE_SAVE_ITEM = 'save_item';
|
||||
export const MODAL_TYPE_EDIT_ITEM = 'edit_item';
|
||||
export const STATE_LOADING = 'loading';
|
||||
export const STATE_ERROR = 'error';
|
||||
export const STATE_SUCCESS = 'success';
|
||||
export const STATE_IDLE = 'idle';
|
||||
|
||||
// Capabilities.
|
||||
export const CAP_PORTABILITY = 'portability';
|
||||
export const CAP_SNIPPET_PORTABILITY = 'et_code_snippets_portability';
|
20
wp-content/themes/Divi/core/code-snippets/app/lib/i18n.js
Normal file
20
wp-content/themes/Divi/core/code-snippets/app/lib/i18n.js
Normal file
@@ -0,0 +1,20 @@
|
||||
// External dependencies.
|
||||
import { get } from 'lodash';
|
||||
|
||||
const i18n = (context, key, ...args) => {
|
||||
const value = get(context, key, '');
|
||||
|
||||
if ('production' !== process.env.NODE_ENV && '' === value) {
|
||||
console.error('Failed to find i18n string:', key);
|
||||
}
|
||||
|
||||
if (args.length > 0) {
|
||||
const sprintf = get(window, 'wp.i18n.sprintf');
|
||||
return sprintf(value, ...args);
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
|
||||
export default (path, key, ...args) => i18n(window.et_code_snippets_data.i18n, [path, key], ...args);
|
41
wp-content/themes/Divi/core/code-snippets/app/lib/request.js
Normal file
41
wp-content/themes/Divi/core/code-snippets/app/lib/request.js
Normal file
@@ -0,0 +1,41 @@
|
||||
// External dependencies.
|
||||
import $ from 'jquery';
|
||||
|
||||
// Internal dependencies.
|
||||
import config from './config';
|
||||
|
||||
|
||||
export const request = (method, data, options = {}) => {
|
||||
const deferred = $.ajax({
|
||||
type: method,
|
||||
url: config.api,
|
||||
dataType: 'json',
|
||||
data,
|
||||
...options,
|
||||
});
|
||||
|
||||
return Promise.resolve(deferred.promise())
|
||||
.then(response => {
|
||||
if (false === response.success) {
|
||||
return Promise.reject(response.data || {});
|
||||
}
|
||||
return Promise.resolve(response.data);
|
||||
});
|
||||
};
|
||||
|
||||
export const post = (data, options = {}) => request('POST', data, options);
|
||||
|
||||
export const download = url => {
|
||||
const deferred = $.ajax({
|
||||
type: 'GET',
|
||||
url,
|
||||
});
|
||||
|
||||
return Promise.resolve(deferred.promise())
|
||||
.then(response => {
|
||||
if (false === response.success) {
|
||||
return Promise.reject(response || {});
|
||||
}
|
||||
return Promise.resolve(response);
|
||||
});
|
||||
};
|
@@ -0,0 +1,20 @@
|
||||
function insertCodeIntoField({ get, props: { snippet, codeMirrorId, isAppend, skipInsert } }) {
|
||||
if (skipInsert) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const codeMirrorInstance = jQuery(`#${codeMirrorId}`).next('.CodeMirror')[0].CodeMirror;
|
||||
|
||||
// Insert code into specified codeMirror field.
|
||||
// Append or replace depending on user preferences.
|
||||
if (isAppend) {
|
||||
snippet = codeMirrorInstance.getValue() ? '\n' + snippet : snippet;
|
||||
return Promise.resolve(codeMirrorInstance.replaceRange(snippet, {line: codeMirrorInstance.lastLine()}));
|
||||
} else {
|
||||
return Promise.resolve(codeMirrorInstance.setValue(snippet));
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
insertCodeIntoField,
|
||||
};
|
@@ -0,0 +1,15 @@
|
||||
// External dependencies.
|
||||
import { state } from 'cerebral';
|
||||
|
||||
// Internal dependencies.
|
||||
import { MODAL_TYPE_EDIT_ITEM } from '@code-snippets/lib/constants';
|
||||
|
||||
|
||||
const isEditItemModalOpen = get => {
|
||||
const openModal = get(state`openModal`);
|
||||
return openModal === MODAL_TYPE_EDIT_ITEM;
|
||||
};
|
||||
|
||||
export {
|
||||
isEditItemModalOpen,
|
||||
};
|
@@ -0,0 +1,11 @@
|
||||
import { state } from 'cerebral';
|
||||
|
||||
|
||||
const openModal = type => ({ store }) => store.set(state`openModal`, type);
|
||||
|
||||
const closeModal = () => ({ store }) => store.set(state`openModal`, null);
|
||||
|
||||
export {
|
||||
openModal,
|
||||
closeModal,
|
||||
};
|
@@ -0,0 +1,23 @@
|
||||
import edit from '@code-snippets/store/edit/module';
|
||||
import * as providers from './providers';
|
||||
import * as sequences from './sequences';
|
||||
import * as computed from './computed';
|
||||
|
||||
export default initialState => (
|
||||
{
|
||||
state: {
|
||||
...initialState,
|
||||
openModal: null,
|
||||
itemsLoadedAndCached: false,
|
||||
computed: {
|
||||
...computed,
|
||||
},
|
||||
cloudToken: null,
|
||||
},
|
||||
providers,
|
||||
sequences,
|
||||
modules: {
|
||||
edit,
|
||||
},
|
||||
}
|
||||
);
|
@@ -0,0 +1,155 @@
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
import { post } from '../../lib/request';
|
||||
import config from '../../lib/config';
|
||||
|
||||
|
||||
export const codeSnippetsLibApi = {
|
||||
/**
|
||||
* Gets the Code Snippet library items.
|
||||
*
|
||||
* @param {string} type One of `et_code_snippet` types.
|
||||
* @returns {Array} Resolved value from promise. Array of objects.
|
||||
*/
|
||||
getItems(type) {
|
||||
/* eslint-disable key-spacing */
|
||||
return post({
|
||||
action : 'et_code_snippets_library_get_items',
|
||||
nonce : config.nonces.et_code_snippets_library_get_items,
|
||||
et_code_snippet_type: type,
|
||||
});
|
||||
/* eslint-enable */
|
||||
},
|
||||
|
||||
getItemContent(id, snippetType, contentFormat = 'raw') {
|
||||
/* eslint-disable key-spacing */
|
||||
return post({
|
||||
action : 'et_code_snippets_library_get_item_content',
|
||||
nonce : config.nonces.et_code_snippets_library_get_item_content,
|
||||
et_code_snippet_id : id,
|
||||
et_code_snippet_type : snippetType,
|
||||
et_code_snippet_format : contentFormat,
|
||||
});
|
||||
/* eslint-enable */
|
||||
},
|
||||
|
||||
saveItemContent(id, snippetContent) {
|
||||
/* eslint-disable key-spacing */
|
||||
return post({
|
||||
action : 'et_code_snippets_library_save_item_content',
|
||||
nonce : config.nonces.et_code_snippets_library_save_item_content,
|
||||
et_code_snippet_id : id,
|
||||
et_code_snippet_content: snippetContent,
|
||||
});
|
||||
/* eslint-enable */
|
||||
},
|
||||
|
||||
removeLocalItem(id) {
|
||||
/* eslint-disable key-spacing */
|
||||
return post({
|
||||
action : 'et_code_snippets_toggle_cloud_status',
|
||||
nonce : config.nonces.et_code_snippets_library_toggle_item_location,
|
||||
et_code_snippet_id : id,
|
||||
});
|
||||
/* eslint-enable */
|
||||
},
|
||||
|
||||
/**
|
||||
* Update the Code Snippet library item.
|
||||
*
|
||||
* @param {object} payload Updated item details.
|
||||
* @returns {Array} Resolved value from promise. Array of objects.
|
||||
*/
|
||||
updateItem(payload) {
|
||||
/* eslint-disable key-spacing */
|
||||
return post({
|
||||
action : 'et_code_snippets_library_update_item',
|
||||
nonce : config.nonces.et_code_snippets_library_update_item,
|
||||
payload,
|
||||
});
|
||||
/* eslint-enable */
|
||||
},
|
||||
|
||||
/**
|
||||
* Export Code Snippet library item.
|
||||
*
|
||||
* @param {int} id Item ID.
|
||||
* @param {array} cloudContent Cloud content.
|
||||
* @param {obj} directExport Snippet type and Content.
|
||||
* @returns {Array} Resolved value from promise. Array of objects.
|
||||
*/
|
||||
exportItem(id, cloudContent, directExport) {
|
||||
/* eslint-disable key-spacing */
|
||||
return post({
|
||||
action : 'et_code_snippets_library_export_item',
|
||||
nonce : config.nonces.et_code_snippets_library_export_item,
|
||||
id,
|
||||
cloudContent,
|
||||
directExport,
|
||||
});
|
||||
/* eslint-enable */
|
||||
},
|
||||
|
||||
/**
|
||||
* Download Code Snippet library item.
|
||||
*
|
||||
* @param {int} id Item ID.
|
||||
* @param {string} fileName Item name.
|
||||
* @returns {string} URI string.
|
||||
*/
|
||||
downloadExportFile(id, fileName) {
|
||||
/* eslint-disable key-spacing */
|
||||
const args = {
|
||||
action : 'et_code_snippets_library_export_item_download',
|
||||
nonce : config.nonces.et_code_snippets_library_export_item,
|
||||
fileName,
|
||||
id,
|
||||
};
|
||||
|
||||
return `${config.api}?${jQuery.param(args)}`;
|
||||
/* eslint-enable */
|
||||
},
|
||||
|
||||
/**
|
||||
* Import Code Snippet library item.
|
||||
*
|
||||
* @param {Blob} file File.
|
||||
* @returns {Array} Response.
|
||||
*/
|
||||
importItem(fileData) {
|
||||
const fileContent = JSON.parse(fileData.content);
|
||||
|
||||
return post({
|
||||
action : 'et_code_snippets_library_import_item',
|
||||
nonce : config.nonces.et_code_snippets_library_import_item,
|
||||
fileContent: JSON.stringify(fileContent.data),
|
||||
fileData,
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Update Local Tags/Categories and return updated list.
|
||||
*
|
||||
* @param {Blob} file File.
|
||||
* @returns {Array} Response.
|
||||
*/
|
||||
updateFilters(payload) {
|
||||
return post({
|
||||
action : 'et_code_snippets_library_update_terms',
|
||||
nonce : config.nonces.et_code_snippets_library_update_terms,
|
||||
payload,
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieve Cloud Token.
|
||||
*
|
||||
* @returns {Array} Response with cloud token.
|
||||
*/
|
||||
getCloudToken() {
|
||||
return post({
|
||||
action : 'et_code_snippets_library_get_token',
|
||||
nonce : config.nonces.et_code_snippets_library_get_token,
|
||||
});
|
||||
}
|
||||
};
|
||||
/* eslint-enable */
|
@@ -0,0 +1,361 @@
|
||||
// External dependencies.
|
||||
import {
|
||||
props,
|
||||
sequence,
|
||||
state,
|
||||
} from 'cerebral';
|
||||
import { set } from 'cerebral/factories';
|
||||
import { isEmpty } from 'lodash';
|
||||
|
||||
// Internal dependencies.
|
||||
import { getItemTypeByContext, getContextByItemType } from '@common-ui/lib/local-library';
|
||||
import { updateTokens, doApiRequest } from '@cloud/app/lib/api';
|
||||
import { handleCloudError } from '@cloud/app/components/app/actions/shared-actions';
|
||||
import { insertCodeIntoField } from './actions';
|
||||
import { setCodeSnippetItemsLoaded, setCodeSnippetsLibraryToken } from '../../lib/code-snippets-library';
|
||||
import { STATE_IDLE, STATE_LOADING, STATE_SUCCESS } from '@code-snippets/lib/constants';
|
||||
|
||||
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
const setLibraryContext = sequence('Set Code Snippets library context', [
|
||||
set(state`context`, props`context`),
|
||||
set(state`itemsLoadedAndCached`, false),
|
||||
]);
|
||||
|
||||
const setCloudToken = sequence('Set cloudToken', [
|
||||
({ get }) => {
|
||||
setCodeSnippetsLibraryToken(get(props`cloudToken`));
|
||||
},
|
||||
]);
|
||||
|
||||
const cacheCloudToken = sequence('Retrieve saved Cloud Access token and save to state', [
|
||||
({ codeSnippetsLibApi, path }) => {
|
||||
return codeSnippetsLibApi.getCloudToken()
|
||||
.then(cloudTokenData => {
|
||||
return path.success({cloudToken: cloudTokenData.accessToken});
|
||||
})
|
||||
.catch(() => path.error());
|
||||
},
|
||||
{
|
||||
success: [
|
||||
setCloudToken
|
||||
],
|
||||
error: [],
|
||||
},
|
||||
]);
|
||||
|
||||
const loadItems = sequence('Load Code Snippets library items', [
|
||||
({ codeSnippetsLibApi, get, path }) => {
|
||||
const context = get(state`context`);
|
||||
const type = getItemTypeByContext(context);
|
||||
|
||||
// Exit if no context provided.
|
||||
if ('' === context) {
|
||||
return path.error();
|
||||
}
|
||||
|
||||
return codeSnippetsLibApi.getItems(type)
|
||||
.then(response => path.success({
|
||||
items: response,
|
||||
}))
|
||||
.catch(() => path.error());
|
||||
},
|
||||
{
|
||||
success: [
|
||||
set(state`items`, props`items`),
|
||||
|
||||
set(state`itemsLoadedAndCached`, true),
|
||||
|
||||
({get}) => { setCodeSnippetItemsLoaded(get(state`context`), true); },
|
||||
],
|
||||
error: [],
|
||||
},
|
||||
]);
|
||||
|
||||
const insertSnippet = sequence('Insert Code Snippet into a field', [
|
||||
({ codeSnippetsLibApi, get, path }) => {
|
||||
const id = get(props`snippetId`);
|
||||
const snippetContent = get(props`snippetContent`);
|
||||
const context = get(state`context`);
|
||||
const type = getItemTypeByContext(context);
|
||||
|
||||
if (false !== snippetContent) {
|
||||
return path.success({ snippet: snippetContent });
|
||||
}
|
||||
|
||||
return codeSnippetsLibApi.getItemContent(id, type)
|
||||
.then(response => path.success({ snippet: response.snippet }))
|
||||
.catch(() => path.error());
|
||||
},
|
||||
{
|
||||
success: [
|
||||
set(state`snippetCode`, props`snippet`),
|
||||
set(state`snippetCodeAppend`, props`isAppend`),
|
||||
insertCodeIntoField,
|
||||
set(state`showLibrary`, false),
|
||||
set(state`context`, ''),
|
||||
() => { jQuery(window).trigger('et_code_snippets_library_close'); },
|
||||
],
|
||||
error: [],
|
||||
},
|
||||
]);
|
||||
|
||||
const getExportedItem = sequence('Get the exported Code Snippet content', [
|
||||
({ codeSnippetsLibApi, path, props: { id, itemType } }) => {
|
||||
|
||||
return codeSnippetsLibApi.getItemContent(id, itemType, 'exported')
|
||||
.then((response => path.success(response)))
|
||||
.catch(() => path.error());
|
||||
},
|
||||
{
|
||||
success: [Promise.resolve(props`response`)],
|
||||
error: [],
|
||||
},
|
||||
]);
|
||||
|
||||
const toggleLibraryItemLocation = sequence('Remove local item from WPDB', [
|
||||
({ codeSnippetsLibApi, path, props: { id } }) => {
|
||||
|
||||
return codeSnippetsLibApi.removeLocalItem(id)
|
||||
.then((response => path.success(response)))
|
||||
.catch(() => path.error());
|
||||
},
|
||||
{
|
||||
success: [Promise.resolve(props`response`)],
|
||||
error: [],
|
||||
},
|
||||
]);
|
||||
|
||||
const updateLocalFilters = sequence('Update Local Filters', [
|
||||
({ codeSnippetsLibApi, path, props: { payload } }) => {
|
||||
|
||||
return codeSnippetsLibApi.updateFilters(payload)
|
||||
.then((response => path.success(response)))
|
||||
.catch(() => path.error());
|
||||
},
|
||||
{
|
||||
success: [Promise.resolve(props`response`)],
|
||||
error: [],
|
||||
},
|
||||
]);
|
||||
|
||||
const updateItem = sequence('Update Code Snippets library item', [
|
||||
({ codeSnippetsLibApi, get, path }) => {
|
||||
const payload = get(props`payload`);
|
||||
|
||||
return codeSnippetsLibApi.updateItem(payload)
|
||||
.then(response => path.success({
|
||||
updatedItem: {
|
||||
success: true,
|
||||
data: response,
|
||||
},
|
||||
}))
|
||||
.catch(() => path.error());
|
||||
},
|
||||
{
|
||||
success: [Promise.resolve(props`updatedItem`)],
|
||||
error: []
|
||||
},
|
||||
]);
|
||||
|
||||
const setShowLibrary = sequence('Set Code Snippets show library', [
|
||||
set(state`showLibrary`, props`toggle`),
|
||||
]);
|
||||
|
||||
const resetSnippetCode = sequence('Reset the saved code value', [
|
||||
set(state`snippetCode`, ''),
|
||||
]);
|
||||
|
||||
const setShowSaveModal = sequence('Toggle Code Snippets save modal', [
|
||||
set(state`showSave`, props`toggle`),
|
||||
]);
|
||||
|
||||
const openPortablity = sequence('Open Code Snippets portability modal', [
|
||||
set(state`showPortability`, true),
|
||||
]);
|
||||
|
||||
const closePortability = sequence('Close Code Snippets portability modal', [
|
||||
set(state`showPortability`, false),
|
||||
set(state`importError`, false),
|
||||
]);
|
||||
|
||||
const exportSnippet = sequence('Export code snippet', [
|
||||
({ codeSnippetsLibApi, path, props: { id, cloudContent, directExport } }) => {
|
||||
return codeSnippetsLibApi.exportItem(id, cloudContent, directExport)
|
||||
.then(() => path.success())
|
||||
.catch(() => path.error());
|
||||
},
|
||||
{
|
||||
success: [
|
||||
({ codeSnippetsLibApi, props: { id, fileName } }) => {
|
||||
const downloadURI = codeSnippetsLibApi.downloadExportFile(id, fileName);
|
||||
|
||||
window.location.assign(downloadURI);
|
||||
|
||||
window.ETCloudApp.emitSignal({
|
||||
signal: 'finishDownload',
|
||||
data: {},
|
||||
});
|
||||
},
|
||||
|
||||
({ props: { directExport } }) => {
|
||||
if (!isEmpty(directExport)) {
|
||||
jQuery(window).trigger('et_code_snippets_library_close');
|
||||
}
|
||||
},
|
||||
|
||||
closePortability,
|
||||
],
|
||||
error: []
|
||||
},
|
||||
]);
|
||||
|
||||
const importSnippetToCloud = sequence('Import code snippet to cloud', [
|
||||
({ get, store, props: { importFile } }) => updateTokens().then(refreshResponse => {
|
||||
const accessToken = refreshResponse['accessToken'];
|
||||
const callback = (newItem) => {
|
||||
if (newItem.error) {
|
||||
handleCloudError(newItem, get, store, null);
|
||||
return;
|
||||
}
|
||||
|
||||
store.set(state`showPortability`, false);
|
||||
store.set(state`importError`, false),
|
||||
store.set(state`importState`, STATE_SUCCESS),
|
||||
|
||||
// Trigger Cloud Items Refresh in the cloud snippet library modal.
|
||||
window.ETCloudApp.emitSignal({
|
||||
signal: 'refreshCloudItems',
|
||||
data: {},
|
||||
});
|
||||
};
|
||||
|
||||
if (!isEmpty(accessToken)) {
|
||||
store.set(state`cloudToken`, accessToken);
|
||||
|
||||
const newCloudItem = {
|
||||
title : importFile.title,
|
||||
content: importFile.content,
|
||||
status : 'publish',
|
||||
meta : {},
|
||||
};
|
||||
|
||||
const resource = getContextByItemType(importFile.type);
|
||||
const providedBaseUrl = window.ETCloudApp.getActiveFolderEndpoint();
|
||||
doApiRequest({ type: 'post', resource, accessToken, providedBaseUrl }, newCloudItem).then(callback);
|
||||
} else {
|
||||
set(state`importState`, STATE_IDLE),
|
||||
store.set(state`cloudStatus`, { error: 'auth_error' });
|
||||
}
|
||||
}),
|
||||
]);
|
||||
|
||||
const importSnippetToLocal = sequence('Import code snippet to local', [
|
||||
({ codeSnippetsLibApi, path, props: { importFile } }) => {
|
||||
return codeSnippetsLibApi.importItem(importFile)
|
||||
.then(() => path.success())
|
||||
.catch(() => path.error());
|
||||
},
|
||||
{
|
||||
success: [
|
||||
closePortability,
|
||||
set(state`importState`, STATE_SUCCESS),
|
||||
() => { jQuery(window).trigger('et_cloud_refresh_local_items'); },
|
||||
],
|
||||
error: [
|
||||
set(state`importState`, STATE_IDLE),
|
||||
set(state`importError`, true),
|
||||
]
|
||||
},
|
||||
]);
|
||||
|
||||
const decideSnippetImport = sequence('Decide import code snippet', [
|
||||
set(state`importState`, STATE_LOADING),
|
||||
|
||||
({ path, props: { importToCloud } }) => {
|
||||
if (importToCloud) {
|
||||
return path.cloud();
|
||||
} else {
|
||||
return path.local();
|
||||
}
|
||||
},
|
||||
{
|
||||
cloud: [
|
||||
importSnippetToCloud,
|
||||
],
|
||||
local: [
|
||||
importSnippetToLocal,
|
||||
],
|
||||
}
|
||||
]);
|
||||
|
||||
const importSnippet = sequence('Import code snippet', [
|
||||
({ path, props: { importFile } }) => {
|
||||
if (importFile) return path.yes();
|
||||
},
|
||||
{
|
||||
yes: [
|
||||
decideSnippetImport,
|
||||
],
|
||||
}
|
||||
]);
|
||||
|
||||
const downloadSnippetContent = sequence('Download Snippet Content', [
|
||||
() => window.ETCloudApp.setCodeSnippetPreviewState({ codeSnippetPreviewState: STATE_LOADING }),
|
||||
// eslint-disable-next-line no-shadow
|
||||
({ codeSnippetsLibApi, get, path, props: { snippetId, snippetContent, needImageRefresh, item_location = '' } }) => {
|
||||
// When a local item is downloaded, snippetId shall be available.
|
||||
if (! snippetContent && 'cloud' !== item_location ) {
|
||||
const context = get(state`context`);
|
||||
const type = getItemTypeByContext(context);
|
||||
|
||||
return codeSnippetsLibApi.getItemContent(snippetId, type)
|
||||
.then(response => path.success({ snippet: response.snippet, itemId: snippetId }))
|
||||
.catch(() => path.error());
|
||||
}
|
||||
|
||||
// When a Cloud item is downloaded, snippetContent shall be available.
|
||||
const snippet = snippetContent;
|
||||
return path.success({ snippet, itemId: snippetId, needImageRefresh });
|
||||
},
|
||||
{
|
||||
success: [
|
||||
({ props: { snippet, itemId, needImageRefresh } }) => {
|
||||
window.ETCloudApp.emitSignal({
|
||||
signal: 'renderCodeSnippetPreview',
|
||||
data: { snippet, itemId, needImageRefresh },
|
||||
});
|
||||
},
|
||||
],
|
||||
error: [
|
||||
|
||||
],
|
||||
},
|
||||
() => window.ETCloudApp.setCodeSnippetPreviewState({ codeSnippetPreviewState: '' }),
|
||||
]);
|
||||
|
||||
const closeModal = sequence('Close open modal', [
|
||||
set(state`openModal`, null),
|
||||
]);
|
||||
|
||||
export {
|
||||
closePortability,
|
||||
downloadSnippetContent,
|
||||
exportSnippet,
|
||||
getExportedItem,
|
||||
importSnippet,
|
||||
insertSnippet,
|
||||
loadItems,
|
||||
openPortablity,
|
||||
resetSnippetCode,
|
||||
setLibraryContext,
|
||||
setShowLibrary,
|
||||
setShowSaveModal,
|
||||
toggleLibraryItemLocation,
|
||||
updateItem,
|
||||
updateLocalFilters,
|
||||
closeModal,
|
||||
setCloudToken,
|
||||
cacheCloudToken,
|
||||
};
|
||||
/* eslint-enable */
|
@@ -0,0 +1,37 @@
|
||||
// External dependencies.
|
||||
import {
|
||||
state,
|
||||
} from 'cerebral';
|
||||
import { noop } from 'lodash';
|
||||
|
||||
// Internal dependencies.
|
||||
import { saveToCloudPure } from '@cloud/app/lib/api';
|
||||
|
||||
|
||||
const saveToCloud = ({ get, props, path }) => {
|
||||
const itemId = get(state`edit.item.id`);
|
||||
const context = get(state`context`);
|
||||
let snippetContent = get(state`edit.content`);
|
||||
|
||||
snippetContent = {
|
||||
...snippetContent,
|
||||
data: props.content,
|
||||
};
|
||||
|
||||
|
||||
return saveToCloudPure(context, { content: JSON.stringify(snippetContent) }, [], noop, itemId)
|
||||
.then(() => path.success())
|
||||
.catch(() => path.error());
|
||||
};
|
||||
|
||||
const saveToLocal = ({ codeSnippetsLibApi, path, props: { content }, get }) => {
|
||||
const itemId = get(state`edit.item.id`);
|
||||
return codeSnippetsLibApi.saveItemContent(itemId, content)
|
||||
.then(response => path.success({ snippet: response.snippet }))
|
||||
.catch(() => path.error());
|
||||
};
|
||||
|
||||
export {
|
||||
saveToCloud,
|
||||
saveToLocal,
|
||||
};
|
@@ -0,0 +1,15 @@
|
||||
// Internal dependencies.
|
||||
import * as sequences from './sequences';
|
||||
|
||||
|
||||
export default {
|
||||
state: {
|
||||
item: null,
|
||||
snippet: '',
|
||||
saveState: null,
|
||||
progress: 0,
|
||||
content: {},
|
||||
context: '',
|
||||
},
|
||||
sequences,
|
||||
};
|
@@ -0,0 +1,133 @@
|
||||
// External dependencies.
|
||||
import {
|
||||
props,
|
||||
sequence,
|
||||
state,
|
||||
} from 'cerebral';
|
||||
import {
|
||||
set,
|
||||
wait,
|
||||
when,
|
||||
} from 'cerebral/factories';
|
||||
|
||||
// Internal dependencies.
|
||||
import {
|
||||
MODAL_TYPE_EDIT_ITEM,
|
||||
STATE_ERROR,
|
||||
STATE_LOADING,
|
||||
STATE_SUCCESS,
|
||||
} from '@code-snippets/lib/constants';
|
||||
import {
|
||||
closeModal,
|
||||
openModal,
|
||||
} from '@code-snippets/store/code-snippets-library/factories';
|
||||
import { downloadSnippetContent } from '@code-snippets/store/code-snippets-library/sequences';
|
||||
import {
|
||||
saveToCloud,
|
||||
saveToLocal,
|
||||
} from './actions';
|
||||
|
||||
|
||||
const openLocalItemEditor = sequence('Open local item editor', [
|
||||
set(state`edit.progress`, 10),
|
||||
({ codeSnippetsLibApi, path, props: { item } }) => codeSnippetsLibApi.getItemContent(item.id, item.type)
|
||||
.then(response => path.success({ snippet: response.snippet }))
|
||||
.catch(() => path.error()),
|
||||
{
|
||||
success: [
|
||||
set(state`edit.progress`, 90),
|
||||
wait(500),
|
||||
set(state`edit.progress`, 100),
|
||||
wait(200),
|
||||
({ store, props: { snippet } }) => {
|
||||
store.set(state`edit.snippet`, snippet);
|
||||
},
|
||||
set(state`edit.progress`, 0),
|
||||
],
|
||||
error: [
|
||||
set(state`edit.progress`, 0),
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
const openCloudItemEditor = sequence('Open cloud item editor', [
|
||||
set(state`edit.snippet`, props`content.data`),
|
||||
set(state`edit.content`, props`content`),
|
||||
]);
|
||||
|
||||
const openEditModal = sequence('Open modal to edit code snippet', [
|
||||
openModal(MODAL_TYPE_EDIT_ITEM),
|
||||
set(state`edit.item`, props`item`),
|
||||
set(state`edit.context`, props`context`),
|
||||
when(props`item.item_location`, item_location => 'cloud' === item_location),
|
||||
{
|
||||
true: [
|
||||
openCloudItemEditor,
|
||||
],
|
||||
false: [
|
||||
openLocalItemEditor,
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
const closeEditModal = sequence('Close snippet editor modal', [
|
||||
closeModal(),
|
||||
set(state`edit.saveState`, null),
|
||||
set(state`edit.item`, null),
|
||||
set(state`edit.snippet`, ''),
|
||||
]);
|
||||
|
||||
|
||||
const saveEditedItemSuccess = sequence('Edited item Saved', [
|
||||
() => ({needImageRefresh: true}),
|
||||
downloadSnippetContent,
|
||||
set(state`edit.saveState`, STATE_SUCCESS),
|
||||
wait(500),
|
||||
closeEditModal,
|
||||
]);
|
||||
|
||||
const saveEditedItemError = sequence('Error while saving edited item', [
|
||||
set(state`edit.saveState`, STATE_ERROR),
|
||||
wait(500),
|
||||
closeEditModal,
|
||||
]);
|
||||
|
||||
const saveEditedContent = sequence('Save editted content', [
|
||||
set(state`edit.saveState`, STATE_LOADING),
|
||||
when(state`edit.item.item_location`, item_location => 'cloud' === item_location),
|
||||
{
|
||||
true: [
|
||||
saveToCloud,
|
||||
{
|
||||
success: [
|
||||
({ get, props: { content } }) => ({ snippetId: get(state`edit.item.id`), snippetContent: content }),
|
||||
({ get }) => ({needImageRefresh: true, item_location: get(state`edit.item.item_location`)}),
|
||||
saveEditedItemSuccess,
|
||||
],
|
||||
error: [
|
||||
saveEditedItemError,
|
||||
],
|
||||
},
|
||||
],
|
||||
false: [
|
||||
saveToLocal,
|
||||
{
|
||||
success: [
|
||||
({ get }) => ({ snippetId: get(state`edit.item.id`) }),
|
||||
saveEditedItemSuccess,
|
||||
],
|
||||
error: [
|
||||
saveEditedItemError,
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
]);
|
||||
|
||||
|
||||
export {
|
||||
openEditModal,
|
||||
closeEditModal,
|
||||
saveEditedContent,
|
||||
};
|
@@ -0,0 +1,5 @@
|
||||
// Internal dependencies.
|
||||
import codeSnippetsLibrary from './code-snippets-library/module';
|
||||
|
||||
|
||||
export default codeSnippetsLibrary;
|
Reference in New Issue
Block a user