Notes: Add editing (#58)

Closes #58
This commit is contained in:
Ilya Prokopenko 2022-01-26 18:20:10 +03:00
parent b72e80f212
commit 870653ccb1
No known key found for this signature in database
GPG key ID: 7736BBBB05F14A56
7 changed files with 92 additions and 0 deletions

View file

@ -94,4 +94,9 @@ class Note extends Postable
return $cached; return $cached;
} }
function getSource(): string
{
return $this->getRecord()->source;
}
} }

View file

@ -67,6 +67,35 @@ final class NotesPresenter extends OpenVKPresenter
$note->setCreated(time()); $note->setCreated(time());
$note->setName($this->postParam("name")); $note->setName($this->postParam("name"));
$note->setSource($this->postParam("html")); $note->setSource($this->postParam("html"));
$note->setEdited(time());
$note->save();
$this->redirect("/note" . $this->user->id . "_" . $note->getVirtualId());
}
}
function renderEdit(int $owner, int $note_id): void
{
$this->assertUserLoggedIn();
$this->willExecuteWriteAction();
$note = $this->notes->getNoteById($owner, $note_id);
if(!$note || $note->getOwner()->getId() !== $owner || $note->isDeleted())
$this->notFound();
if(is_null($this->user) || !$note->canBeModifiedBy($this->user->identity))
$this->flashFail("err", "Ошибка доступа", "Недостаточно прав для модификации данного ресурса.");
$this->template->note = $note;
if($_SERVER["REQUEST_METHOD"] === "POST") {
if(empty($this->postParam("name"))) {
$this->flashFail("err", tr("error"), tr("error_segmentation"));
}
$note->setName($this->postParam("name"));
$note->setSource($this->postParam("html"));
$note->setCached_Content(NULL);
$note->setEdited(time());
$note->save(); $note->save();
$this->redirect("/note" . $this->user->id . "_" . $note->getVirtualId()); $this->redirect("/note" . $this->user->id . "_" . $note->getVirtualId());

View file

@ -0,0 +1,47 @@
{extends "../@layout.xml"}
{block title}{_edit_note}{/block}
{block header}
{var author = $note->getOwner()}
<a href="{$author->getURL()}">{$author->getCanonicalName()}</a>
»
<a href="/notes{$author->getId()}">{_notes}</a>
»
<a href="/note{$author->getId()}_{$note->getVirtualId()}">{$note->getName()}</a>
{/block}
{block content}
<form id="noteFactory" method="POST">
<input type="text" name="name" placeholder="{_name_note}" style="width:603px;" value="{$note->getName()}" />
<br/><br/>
<textarea name="html" style="display:none;"></textarea>
<div id="editor" style="width:600px;height:300px;border:1px solid grey"></div>
<p><i><a href="/kb/notes">Кое-что</a> из (X)HTML поддерживается.</i></p>
<input type="hidden" name="hash" value="{$csrfToken}" />
<button class="button">{_save}</button>
</form>
{script "js/node_modules/monaco-editor/min/vs/loader.js"}
{script "js/node_modules/requirejs/bin/r.js"}
<script>
require.config({
paths: {
'vs': '/assets/packages/static/openvk/js/node_modules/monaco-editor/min/vs'
}
});
require(['vs/editor/editor.main'], function() {
window._editor = monaco.editor.create(document.getElementById('editor'), {
value: {$note->getSource()},
lineNumbers: "off",
language: "html"
});
});
document.querySelector("#noteFactory").addEventListener("submit", function() {
document.querySelector("textarea").value = window._editor.getValue();
});
</script>
{/block}

View file

@ -44,6 +44,7 @@
</div> </div>
<div class="byline"> <div class="byline">
<span><a href="{$author->getURL()}">{$author->getCanonicalName()}</a></span> {$note->getPublicationTime()} <span><a href="{$author->getURL()}">{$author->getCanonicalName()}</a></span> {$note->getPublicationTime()}
<span n:if="$note->getEditTime() > $note->getPublicationTime()">({_edited} {$note->getEditTime()})</span>
</div> </div>
</div> </div>
<div style="margin-left: 6px; width: 535px;"> <div style="margin-left: 6px; width: 535px;">
@ -60,6 +61,8 @@
{/if} {/if}
<span n:if="isset($thisUser) && $thisUser->getId() === $note->getOwner()->getId()">&nbsp;|&nbsp; <span n:if="isset($thisUser) && $thisUser->getId() === $note->getOwner()->getId()">&nbsp;|&nbsp;
<a id="_noteDelete" href="/note{$note->getOwner()->getId()}_{$note->getId()}/delete">{_delete}</a> <a id="_noteDelete" href="/note{$note->getOwner()->getId()}_{$note->getId()}/delete">{_delete}</a>
&nbsp;|&nbsp;
<a href="/note{$note->getOwner()->getId()}_{$note->getVirtualId()}/edit">{_edit}</a>
</span> </span>
</div> </div>
</div> </div>

View file

@ -223,6 +223,8 @@ routes:
handler: "Notes->view" handler: "Notes->view"
- url: "/notes/create" - url: "/notes/create"
handler: "Notes->create" handler: "Notes->create"
- url: "/note{num}_{num}/edit"
handler: "Notes->edit"
- url: "/note{num}_{num}/delete" - url: "/note{num}_{num}/delete"
handler: "Notes->delete" handler: "Notes->delete"
- url: "/invite" - url: "/invite"

View file

@ -295,8 +295,11 @@
"name_note" = "Title"; "name_note" = "Title";
"text_note" = "Content"; "text_note" = "Content";
"create_note" = "Create note"; "create_note" = "Create note";
"edit_note" = "Edit note";
"actions" = "Actions"; "actions" = "Actions";
"edited" = "Edited";
"notes_zero" = "No notes"; "notes_zero" = "No notes";
"notes_one" = "$1 note"; "notes_one" = "$1 note";
"notes_other" = "$1 notes"; "notes_other" = "$1 notes";

View file

@ -313,8 +313,11 @@
"name_note" = "Название"; "name_note" = "Название";
"text_note" = "Содержание"; "text_note" = "Содержание";
"create_note" = "Создать заметку"; "create_note" = "Создать заметку";
"edit_note" = "Редактировать заметку";
"actions" = "Действия"; "actions" = "Действия";
"edited" = "Отредактировано";
"notes_zero" = "Ни одной заметки"; "notes_zero" = "Ни одной заметки";
"notes_one" = "Одна заметка"; "notes_one" = "Одна заметка";
"notes_few" = "$1 заметки"; "notes_few" = "$1 заметки";