diff --git a/Web/Models/Entities/Note.php b/Web/Models/Entities/Note.php index 762ebef8..a536602a 100644 --- a/Web/Models/Entities/Note.php +++ b/Web/Models/Entities/Note.php @@ -74,9 +74,19 @@ class Note extends Postable $config->set("Attr.AllowedClasses", [ "underline", ]); + + $source = NULL; + if(is_null($this->getRecord())) { + if(isset($this->changes["source"])) + $source = $this->changes["source"]; + else + throw new \LogicException("Can't render note without content set."); + } else { + $source = $this->getRecord()->source; + } $purifier = new HTMLPurifier($config); - return $purifier->purify($this->getRecord()->source); + return $purifier->purify($source); } function getName(): string @@ -91,6 +101,9 @@ class Note extends Postable function getText(): string { + if(is_null($this->getRecord())) + return $this->renderHTML(); + $cached = $this->getRecord()->cached_content; if(!$cached) { $cached = $this->renderHTML(); diff --git a/Web/Presenters/NotesPresenter.php b/Web/Presenters/NotesPresenter.php index 2eb75f91..363d814c 100644 --- a/Web/Presenters/NotesPresenter.php +++ b/Web/Presenters/NotesPresenter.php @@ -46,6 +46,29 @@ final class NotesPresenter extends OpenVKPresenter $this->template->note = $note; } + function renderPreView(): void + { + $this->assertUserLoggedIn(); + $this->willExecuteWriteAction(); + + if($_SERVER["REQUEST_METHOD"] !== "POST") { + header("HTTP/1.1 400 Bad Request"); + exit; + } + + if(empty($this->postParam("html")) || empty($this->postParam("title"))) { + header("HTTP/1.1 400 Bad Request"); + exit(tr("note_preview_empty_err")); + } + + $note = new Note; + $note->setSource($this->postParam("html")); + + $this->flash("info", tr("note_preview_warn"), tr("note_preview_warn_details")); + $this->template->title = $this->postParam("title"); + $this->template->html = $note->getText(); + } + function renderCreate(): void { $this->assertUserLoggedIn(); diff --git a/Web/Presenters/templates/Notes/Create.xml b/Web/Presenters/templates/Notes/Create.xml index 5cd1cadb..e1a2bd2e 100644 --- a/Web/Presenters/templates/Notes/Create.xml +++ b/Web/Presenters/templates/Notes/Create.xml @@ -17,6 +17,7 @@ + {_note_preview} {script "js/node_modules/monaco-editor/min/vs/loader.js"} @@ -38,5 +39,20 @@ document.querySelector("#noteFactory").addEventListener("submit", function() { document.querySelector("textarea").value = window._editor.getValue(); }); + + window._preview = undefined; + function openPreviewWindow() { + if(typeof window._preview != "undefined") { + window._preview.close(); + window._preview = undefined; + } + + window._preview = window.open("data:text/html,", "_blank", { popup: true }); + window._preview.document.write(`
`); + window._preview.document.querySelector("input[name=title]").value = document.querySelector("input[name=name]").value; + window._preview.document.querySelector("input[name=html]").value = window._editor.getValue(); + window._preview.document.querySelector("input[name=hash]").value = document.querySelector("meta[name=csrf]").attributes.value.value; + window._preview.document.querySelector("form").submit(); + } {/block} diff --git a/Web/Presenters/templates/Notes/PreView.xml b/Web/Presenters/templates/Notes/PreView.xml new file mode 100644 index 00000000..4f80214d --- /dev/null +++ b/Web/Presenters/templates/Notes/PreView.xml @@ -0,0 +1,37 @@ +{extends "../@layout.xml"} + +{block title} + {$title} +{/block} + +{block header} + {$title} +{/block} + +{block content} + + + {$html|noescape} +{/block} diff --git a/Web/routes.yml b/Web/routes.yml index 3698817a..55225df3 100644 --- a/Web/routes.yml +++ b/Web/routes.yml @@ -241,6 +241,8 @@ routes: handler: "Notes->list" - url: "/note{num}_{num}" handler: "Notes->view" + - url: "/notes/prerender" + handler: "Notes->preView" - url: "/notes/create" handler: "Notes->create" - url: "/note{num}_{num}/edit" diff --git a/locales/en.strings b/locales/en.strings index 30643f84..daa4e27b 100644 --- a/locales/en.strings +++ b/locales/en.strings @@ -357,6 +357,10 @@ "notes_one" = "$1 note"; "notes_other" = "$1 notes"; "notes_start_screen" = "With notes, you can share your events with friends and see what's going on with them."; +"note_preview" = "Preview draft"; +"note_preview_warn" = "Preview mode active"; +"note_preview_warn_details" = "Notes may change their behaviour or looks after being saved. Also, try not to use preview very often."; +"note_preview_empty_err" = "Preview of empty or nameless note? Why, Mr. White?"; "notes_list_zero" = "No notes found"; "notes_list_one" = "$1 note found"; diff --git a/locales/ru.strings b/locales/ru.strings index a654e0dd..af93c4db 100644 --- a/locales/ru.strings +++ b/locales/ru.strings @@ -383,6 +383,10 @@ "edit_note" = "Редактировать заметку"; "actions" = "Действия"; "notes_start_screen" = "С помощью заметок Вы можете делиться событиями из жизни с друзьями, а так же быть в курсе того, что происходит у них."; +"note_preview" = "Предпросмотр"; +"note_preview_warn" = "Это всего лишь предпросмотр"; +"note_preview_warn_details" = "После сохранения заметки могут выглядеть иначе. К тому же, не вызывайте предпросмотр слишком часто."; +"note_preview_empty_err" = "Зачем вам предпросмотр для заметки без имени или содержания?"; "edited" = "Отредактировано";