Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-pg committed Jul 13, 2023
1 parent f7dfe2f commit cf70623
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 62 deletions.
101 changes: 53 additions & 48 deletions admin/symbols/manager.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

class Brizy_Admin_Symbols_Manager {
class Brizy_Admin_Symbols_Manager
{
const BRIZY_SYMBOLS_KEY = 'brizy-symbols';

/**
Expand All @@ -10,16 +11,16 @@ class Brizy_Admin_Symbols_Manager {
* @return Brizy_Admin_Symbols_Symbol
* @throws Exception
*/
public function createFromJson( $jsonString ) {
$jsonObj = json_decode( $jsonString );
public function createFromJson($jsonString)
{
$jsonObj = json_decode($jsonString);
$result = [];
if ( is_array( $jsonObj ) ) {
foreach ( $jsonObj as $obj ) {
$result[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject( $obj );
if (is_array($jsonObj)) {
foreach ($jsonObj as $obj) {
$result[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject($obj);
}

} elseif(!is_null($jsonObj)) {
$result[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject( $jsonObj );
} elseif (!is_null($jsonObj)) {
$result[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject($jsonObj);
}

return $result;
Expand All @@ -28,13 +29,14 @@ public function createFromJson( $jsonString ) {
/**
* @return Brizy_Admin_Symbols_Symbol[]
*/
public function getList() {
$symbolsEncoded = get_option( self::BRIZY_SYMBOLS_KEY, base64_encode( "[]" ) );
$jsonSymbols = json_decode( base64_decode( $symbolsEncoded ) );
public function getList()
{
$symbolsEncoded = get_option(self::BRIZY_SYMBOLS_KEY, base64_encode("[]"));
$jsonSymbols = json_decode(base64_decode($symbolsEncoded));

$symbols = [];
foreach ( $jsonSymbols as $symbol ) {
$symbols[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject( $symbol );
foreach ($jsonSymbols as $symbol) {
$symbols[] = Brizy_Admin_Symbols_Symbol::createFromJsonObject($symbol);
}

return $symbols;
Expand All @@ -43,12 +45,13 @@ public function getList() {
/**
* @return Brizy_Admin_Symbols_Symbol
*/
public function get( $uid ) {
public function get($uid)
{

$symbols = $this->getList();

foreach ( $symbols as $symbol ) {
if ( $symbol->getUid() == $uid ) {
foreach ($symbols as $symbol) {
if ($symbol->getUid() == $uid) {
return $symbol;
}
}
Expand All @@ -60,77 +63,79 @@ public function get( $uid ) {
/**
* @param Brizy_Admin_Symbols_Symbol $aSymbol
*/
public function deleteSymbol( $aSymbol ) {
if ( ! $aSymbol ) {
throw new Exception( "Unable to delete NULL symbol" );
public function deleteSymbol($aSymbol)
{
if (!$aSymbol) {
throw new Exception("Unable to delete NULL symbol");
}

$symbols = $this->getList();

foreach ( $symbols as $i => $symbol ) {
if ( $symbol->getUid() == $aSymbol->getUid() ) {
unset( $symbols[ $i ] );
foreach ($symbols as $i => $symbol) {
if ($symbol->getUid() == $aSymbol->getUid()) {
unset($symbols[$i]);
}
}

$this->saveAllSymbols( $symbols );
$this->saveAllSymbols($symbols);
}

/**
* @param Brizy_Admin_Symbols_Symbol $aSymbol
*
* @return Brizy_Admin_Symbols_Symbol
*/
public function saveSymbol( $aSymbol ) {
if ( ! $aSymbol ) {
throw new Exception( "Unable to save NULL symbol" );
public function saveSymbol($aSymbol)
{
if (!$aSymbol) {
throw new Exception("Unable to save NULL symbol");
}
$symbols = $this->getList();

foreach ( $symbols as $i => $symbol ) {
if ( $symbol->getUid() == $aSymbol->getUid() ) {
$symbols[ $i ] = $aSymbol;
$this->saveAllSymbols( $symbols );
foreach ($symbols as $i => $symbol) {
if ($symbol->getUid() == $aSymbol->getUid()) {
$symbols[$i] = $aSymbol;
$this->saveAllSymbols($symbols);

return;
}
}
$symbols[] = $aSymbol;
$this->saveAllSymbols( $symbols );
$this->saveAllSymbols($symbols);
}

private function saveAllSymbols( $symbols ) {
update_option( self::BRIZY_SYMBOLS_KEY, base64_encode( json_encode( $symbols ) ) );
private function saveAllSymbols($symbols)
{
update_option(self::BRIZY_SYMBOLS_KEY, base64_encode(json_encode($symbols)));
}

/**
* @param Brizy_Admin_Symbols_Symbol $symbol
*
* @return void
*/
public function validateSymbol( $symbol ) {
if ( is_null( $symbol->getUid() ) || empty( $symbol->getUid() ) ) {
throw new Exception( 'Please provide the symbol uid' );
public function validateSymbol($symbol)
{
if (is_null($symbol->getUid()) || empty($symbol->getUid())) {
throw new Exception('Please provide the symbol uid');
}

if ( is_null( $symbol->getVersion() ) || empty( $symbol->getVersion() ) ) {
throw new Exception( 'Please provide the symbol version' );
if (is_null($symbol->getVersion()) || empty($symbol->getVersion())) {
throw new Exception('Please provide the symbol version');
}

$currentSymbol = $this->get( $symbol->getUid() );
$currentSymbol = $this->get($symbol->getUid());

if ( $currentSymbol && ( $currentSymbol->getVersion() + 1 != $symbol->getVersion() ) ) {
throw new Exception( 'Invalid symbol version. Please refresh and try again.' );
if ($currentSymbol && ($currentSymbol->getVersion() + 1 != $symbol->getVersion())) {
throw new Exception('Invalid symbol version. Please refresh and try again.');
}

if ( is_null( $symbol->getLabel() ) || empty( $symbol->getLabel() ) ) {
throw new Exception( 'Please provide the symbol label' );
if (is_null($symbol->getLabel()) || empty($symbol->getLabel())) {
throw new Exception('Please provide the symbol label');
}

if ( is_null( $symbol->getData() ) || empty( $symbol->getData() ) ) {
throw new Exception( 'Please provide the symbol data' );
if (is_null($symbol->getData()) || empty($symbol->getData())) {
throw new Exception('Please provide the symbol data');
}

}

}
2 changes: 1 addition & 1 deletion brizy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$_SERVER['HTTPS'] = 'on';
}

define('BRIZY_DEVELOPMENT', false);
define('BRIZY_DEVELOPMENT', 1);
define('BRIZY_LOG', false);
define('BRIZY_VERSION', '2.4.26');
define('BRIZY_MINIMUM_PRO_VERSION', '2.4.15');
Expand Down
22 changes: 16 additions & 6 deletions public/editor-client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,14 +813,24 @@ export const deleteSymbol = async (data: ClassSymbol[]): Promise<unknown> => {
});
};

export const updateSymbols = async (store: Readonly<State>) => {
const { toCreate, toUpdate, toDelete } = store.symbols;
export const updateSymbols = async (snap: Readonly<State>) => {
const { toCreate, toUpdate, toDelete } = snap.symbols;

const createPromise = createSymbol(toCreate);
const updatePromise = updateSymbol(toUpdate);
const deletePromise = deleteSymbol(toDelete);
const promises = [];

await Promise.all([createPromise, updatePromise, deletePromise]);
if (toCreate.length) {
promises.push(createSymbol(toCreate));
}

if (toUpdate.length) {
promises.push(updateSymbol(toUpdate));
}

if (toDelete.length) {
promises.push(deleteSymbol(toDelete));
}

await Promise.all(promises);
};

//#endregion
5 changes: 0 additions & 5 deletions public/editor-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ interface Actions {
getPostObjects: string;

searchPosts: string;
getPostObjects: string;

symbolCreate: string;
symbolList: string;
Expand Down Expand Up @@ -186,10 +185,6 @@ const actionsReader = parseStrict<PLUGIN_ENV["actions"], Actions>({
mPipe(Obj.readKey("searchPosts"), Str.read),
throwOnNullish("INvalid actions: searchPosts")
),
getPostObjects: pipe(
mPipe(Obj.readKey("getPostObjects"), Str.read),
throwOnNullish("Invalid actions: getPostObjects")
),
symbolList: pipe(
mPipe(Obj.readKey("symbolList"), Str.read),
throwOnNullish("Invalid actions: symbolList")
Expand Down
23 changes: 22 additions & 1 deletion public/editor-client/src/onChange/symbols.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
import { isSymbol } from "src/symbols/utils";
import { findSymbolIndex, isSymbol } from "src/symbols/utils";
import { SymbolAction } from "src/types/Symbols";
import { store } from "src/valtio";
import { State } from "src/valtio/types";
import { snapshot } from "valtio/vanilla";

export const handleSymbols = (data: SymbolAction): void => {
const { type, payload } = data;

if (type && payload && isSymbol(payload)) {
const state = snapshot(store) as Readonly<State>;

switch (type) {
case "CREATE": {
store.symbols.toCreate.push(payload);
break;
}
case "UPDATE": {
const indexInToCreate = findSymbolIndex("create", payload, state);

if (indexInToCreate !== undefined) {
if (state.symbols.toCreate[indexInToCreate]) {
store.symbols.toCreate[indexInToCreate].data = payload.data;
break;
}
}

const candidateIndex = store.symbols.toUpdate.findIndex(
(s) => s.uid === payload.uid
);

if (candidateIndex === -1) {
store.symbols.toUpdate.push(payload);
break;
}

store.symbols.toUpdate[candidateIndex] = payload;
break;
}
case "DELETE": {
const indexInCreate = findSymbolIndex("create", payload, state);

if (indexInCreate !== undefined && indexInCreate !== -1) {
store.symbols.toCreate.splice(indexInCreate, 1);
break;
}

store.symbols.toDelete.push(payload);
break;
}
Expand Down
30 changes: 29 additions & 1 deletion public/editor-client/src/symbols/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ClassSymbol } from "src/types/Symbols";
import { ClassSymbol, SymbolRequestType } from "src/types/Symbols";
import * as Obj from "src/utils/reader/object";
import { MValue } from "src/utils/types";
import { State } from "src/valtio/types";

export const isSymbol = (s: unknown): s is ClassSymbol => {
const symbol = s as ClassSymbol;
Expand All @@ -12,3 +14,29 @@ export const isSymbol = (s: unknown): s is ClassSymbol => {
Obj.isObject(symbol.data)
);
};

export const findSymbolIndex = (
type: SymbolRequestType,
symbol: ClassSymbol,
state: Readonly<State>
): MValue<number> => {
let arrayToSearch: ClassSymbol[] | undefined = undefined;

switch (type) {
case "create":
arrayToSearch = state.symbols.toCreate;
break;
case "update":
arrayToSearch = state.symbols.toUpdate;
break;
case "delete":
arrayToSearch = state.symbols.toDelete;
break;
}

if (arrayToSearch) {
return arrayToSearch.findIndex((s) => s.uid === symbol.uid);
}

return undefined;
};
2 changes: 2 additions & 0 deletions public/editor-client/src/types/Symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ interface SymbolDelete {
payload: string;
}

export type SymbolRequestType = "create" | "update" | "delete";

export type SymbolAction = SymbolCreate | SymbolUpdate | SymbolDelete;

export interface Symbols {
Expand Down

0 comments on commit cf70623

Please sign in to comment.