This commit is contained in:
flash 2020-10-16 19:24:40 +00:00
parent 2f71fa5e62
commit 2e222021b9
5 changed files with 73 additions and 15 deletions

View file

@ -107,7 +107,7 @@ function ytknsEditorPreview(zoneInfo) {
var formElement = document.createElement('form');
formElement.action = '/zones/_preview';
formElement.method = 'post';
formElement.target = '_blank';
formElement.target = ytknsEditorSBS ? 'preview' : '_blank';
formElement.style.display = 'none';
var elements = ytknsZoneInfoSerialise(zoneInfo, function(name, value) {
@ -154,7 +154,8 @@ var ytknsZoneInfo = null,
ytknsEditorElemMainContainer = null,
ytknsEditorEffects = [],
ytknsEditorIgnoreHashChange = false,
ytknsEditorCleanExit = true;
ytknsEditorCleanExit = true,
ytknsEditorSBS = false;
function ytknsEditorChangeHash(hash) {
ytknsEditorIgnoreHashChange = true;
@ -877,6 +878,9 @@ function ytknsEditorMainSetContainer(child, title) {
ytknsEditorElemMainContainer.innerHTML = '';
ytknsEditorElemMainContainer.appendChild(child);
if(ytknsEditorSBS)
ytknsEditorPreview(ytknsZoneInfo);
}
function ytknsEditorMainClone() {
return {
@ -1025,7 +1029,7 @@ function ytknsEditorHashChange(ev) {
ytknsEditorIgnoreHashChange = false;
}
function ytknsEditorMain(container, zoneId, editorToken, uploadToken) {
function ytknsEditorMain(container, zoneId, editorToken, uploadToken, sideBySide) {
if(navigator.userAgent.match(/mobile/gi) && !confirm("The editor is not designed to be used on phones whatsoever.\r\nHit OK to continue anyway or cancel to whereever you came from.")) {
history.go(-1);
return;
@ -1035,6 +1039,7 @@ function ytknsEditorMain(container, zoneId, editorToken, uploadToken) {
window.onbeforeunload = ytknsEditorBeforeUnload;
ytknsEditorToken = editorToken;
ytknsEditorUploadToken = uploadToken;
ytknsEditorSBS = sideBySide;
container.innerHTML = '';
container.classList.add('ye');

View file

@ -41,6 +41,7 @@ function html_header(array $vars = []): string {
['text' => 'Home', 'link' => page_url('/')],
['text' => 'Create a Zone', 'link' => page_url('/zones/create')],
['text' => 'Zones', 'link' => page_url('/zones')],
['text' => 'Random', 'link' => page_url('/zones/random')],
]);
$userMenu = [];
@ -313,6 +314,12 @@ if($reqPath === '/zones') {
return;
}
if($reqPath === '/zones/random') {
$zoneInfo = Zone::byRandom();
header('Location: ' . $zoneInfo->getUrl());
return;
}
if($reqPath === '/zones/create') {
if(!UserSession::hasInstance()) {
http_response_code(403);
@ -501,6 +508,14 @@ if(preg_match('#^/zones/([0-9]+)/delete$#', $reqPath, $matches)) {
return;
}
if(preg_match('#^/zones/([0-9]+)/sbs$#', $reqPath, $matches)) {
echo '<!doctype html>';
echo '<meta charset="utf-8"/><title>YTKNS Side By Side Editor</title>';
echo '<iframe name="preview" style="position: absolute; top: 0; left: 0; bottom: 0; height: 100%; width: 70%; border-width: 0;"></iframe>';
echo '<iframe name="editor" src="/zones/' . $matches[1] . '" style="position: absolute; top: 0; right: 0; bottom: 0; height: 100%; width: 30%; border-width: 0;"></iframe>';
return;
}
if(preg_match('#^/zones/([0-9]+)$#', $reqPath, $matches)) {
if(!UserSession::hasInstance()) {
http_response_code(403);
@ -523,15 +538,24 @@ if(preg_match('#^/zones/([0-9]+)$#', $reqPath, $matches)) {
return;
}
$isSBS = !empty($_GET['sbs']);
$cssHash = hash_file('sha256', YTKNS_PUB . '/assets/editor.css');
$jsHash = hash_file('sha256', YTKNS_PUB . '/assets/editor.js');
echo html_header([
'title' => 'Editing Zone - YTKNS',
'styles' => [
page_url('/assets/editor.css', ['v' => $cssHash]),
],
]);
if($isSBS) {
echo '<!doctype html>';
echo '<link href="/assets/style.css" type="text/css" rel="stylesheet"/>';
echo '<link href="/assets/editor.css?v=' . $cssHash . '" type="text/css" rel="stylesheet"/>';
echo '<style>.ye { height: 100%; width: 100%; } .ye-sidebar { min-width: 200px; }</style>';
} else {
echo html_header([
'title' => 'Editing Zone - YTKNS',
'styles' => [
page_url('/assets/editor.css', ['v' => $cssHash]),
],
]);
}
Template::render('zones/edit', [
'edit_id' => $zoneInfo->getId(),
'edit_token' => UserSession::instance()->getSmallToken(10),
@ -539,11 +563,24 @@ if(preg_match('#^/zones/([0-9]+)$#', $reqPath, $matches)) {
'edit_js_ver' => substr($jsHash, 0, 16),
'upload_token' => UserSession::instance()->getSmallToken(6),
]);
echo html_footer([
'scripts' => [
page_url('/assets/editor.js', ['v' => $jsHash]),
],
]);
if($isSBS) {
echo '<script type="text/javascript" charset="utf-8" src="/assets/editor.js?v=' . $jsHash . '"></script>';
} else {
echo <<<HTML
<script>
window.addEventListener('DOMContentLoaded', function() {
if(window.location !== window.parent.location)
location.assign(location.toString() + '?sbs=1');
});
</script>
HTML;
echo html_footer([
'scripts' => [
page_url('/assets/editor.js', ['v' => $jsHash]),
],
]);
}
return;
}

View file

@ -6,6 +6,7 @@ use YTKNS\HtmlTag;
use YTKNS\HtmlText;
use YTKNS\PageBuilder;
use YTKNS\PageEffectInterface;
use YTKNS\PageEffectException;
use YTKNS\Upload;
use YTKNS\UploadNotFoundException;

View file

@ -190,6 +190,19 @@ final class Zone {
return $zone;
}
public static function byRandom(): self {
$getZone = DB::prepare('
SELECT `zone_id`, `user_id`, `zone_name`, `zone_title`, `zone_views`,
UNIX_TIMESTAMP(`zone_created`) AS `zone_created`,
UNIX_TIMESTAMP(`zone_updated`) AS `zone_updated`
FROM `ytkns_zones`
ORDER BY RAND()
LIMIT 1
');
$zone = $getZone->execute() ? $getZone->fetchObject(self::class) : null;
return $zone;
}
public static function byName(string $name): self {
$getZone = DB::prepare('
SELECT `zone_id`, `user_id`, `zone_name`, `zone_title`, `zone_views`,

View file

@ -13,7 +13,9 @@
</div>
<script type="text/javascript">
window.onload = function() {
document.getElementById('ytkns-footer').appendChild(document.createTextNode('| JS: :edit_js_ver | CSS: :edit_css_ver'));
var footer = document.getElementById('ytkns-footer');
if(footer)
footer.appendChild(document.createTextNode('| JS: :edit_js_ver | CSS: :edit_css_ver'));
ytknsEditorMain(
document.getElementById('editor'),