Skip to content

Commit

Permalink
Merge pull request #246 from PrestaSafe/paste-local-storage
Browse files Browse the repository at this point in the history
Fix clipboard issue
  • Loading branch information
PrestaSafe authored Aug 30, 2024
2 parents 5c3e8be + 4a75d0d commit 11dcb0b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
76 changes: 32 additions & 44 deletions _dev/src/components/LeftPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,61 +130,49 @@ const copyZone = async () => {
ctx_id_lang: context.id_lang,
ctx_id_shop: context.id_shop,
};
navigator.clipboard.writeText(JSON.stringify(params)).then(function() {
console.log('Copying to clipboard was successful!' );
}, function(err) {
console.error('Could not copy text: ', err);
});
checkClipboardContent()
localStorage.setItem('prettyblocks_clipboard', JSON.stringify(params));
checkClipboardContent();
}
/**
* Paste current zone
*/
const pasteZone = async () => {
let current_zone = prettyBlocksContext.currentZone.name;
const clipboardData = await navigator.clipboard.readText();
const data = JSON.parse(clipboardData);
if (data.hasOwnProperty('zone')) {
let params = {
...data,
zone_name_to_paste: current_zone,
ajax_token: security_app.ajax_token,
ajax: true,
};
HttpClient.post(ajax_urls.state, params).then((response) => {
if (response.success) {
toaster.show(response.message)
// clear clipboard if zone is pasted
navigator.clipboard.writeText('').then(function() {
checkClipboardContent()
}, function(err) {
console.error('Could not empty clipboard: ', err);
checkClipboardContent()
});
prettyBlocksContext.reloadIframe()
prettyBlocksContext.initStates()
}
})
.catch(error => console.error(error));
const storedData = localStorage.getItem('prettyblocks_clipboard');
if (storedData) {
const data = JSON.parse(storedData);
if (data.hasOwnProperty('zone')) {
let params = {
...data,
zone_name_to_paste: current_zone,
ajax_token: security_app.ajax_token,
ajax: true,
};
HttpClient.post(ajax_urls.state, params).then((response) => {
if (response.success) {
toaster.show(response.message);
localStorage.removeItem('prettyblocks_clipboard');
checkClipboardContent();
prettyBlocksContext.reloadIframe();
prettyBlocksContext.initStates();
}
}).catch(error => console.error(error));
}
}
}
let showCopyZone = ref(false);
const checkClipboardContent = async () => {
try {
const clipboardData = await navigator.clipboard.readText();
const data = JSON.parse(clipboardData);
showCopyZone.value = data.hasOwnProperty('zone');
window.blur();
} catch (error) {
showCopyZone.value = false;
try {
const storedData = localStorage.getItem('prettyblocks_clipboard');
if (storedData) {
const data = JSON.parse(storedData);
showCopyZone.value = data.hasOwnProperty('zone');
} else {
showCopyZone.value = false;
}
} catch (error) {
showCopyZone.value = false;
}
};
/**
* Delete all blocks in current zone
*/
Expand Down
24 changes: 24 additions & 0 deletions classes/PrettyBlocksModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ public static function registerBlockToZone($zone_name, $block_code, $id_lang = n
$contextPS = Context::getContext();
$id_lang = ($id_lang !== null) ? (int) $id_lang : $contextPS->language->id;
$id_shop = ($id_shop !== null) ? (int) $id_shop : $contextPS->shop->id;
$maxPosition = self::getMaxPositionByZone($zone_name, $id_lang, $id_shop);

$model = new PrettyBlocksModel(null, $id_lang, $id_shop);
$model->zone_name = $zone_name;
Expand All @@ -880,6 +881,7 @@ public static function registerBlockToZone($zone_name, $block_code, $id_lang = n
$model->state = json_encode($array, true);
$model->instance_id = uniqid();
$model->id_shop = $id_shop;
$model->position = $maxPosition + 1;
$model->save();

$block = $model->mergeStateWithFields();
Expand All @@ -899,6 +901,28 @@ public static function registerBlockToZone($zone_name, $block_code, $id_lang = n
return $block;
}

/**
* getMaxPositionByZone
* get the max position of a zone
*
* @param $zone_name string
* @param $id_lang int
* @param $id_shop int
*
* @return int
*/
public static function getMaxPositionByZone($zone_name, $id_lang, $id_shop)
{
$query = new DbQuery();
$query->select('MAX(position)')
->from('prettyblocks')
->where('`zone_name` = \'' . $zone_name . '\'')
->where('`id_lang` = ' . (int) $id_lang)
->where('`id_shop` = ' . (int) $id_shop);

return (int) DB::getInstance()->getValue($query);
}

/**
* setDefaultConfigValues set default config values
*
Expand Down

0 comments on commit 11dcb0b

Please sign in to comment.