5 Commits

  1. 2
      .gitconfig
  2. 13
      CHANGELOG.md
  3. 59
      assets/js/editor.js
  4. 13
      lib/mirage/markdown.ex
  5. 14
      priv/repo/migrations/20210205175108_add_html_fields.exs

2
.gitconfig

@ -0,0 +1,2 @@
[push]
followTags = true

13
CHANGELOG.md

@ -5,6 +5,19 @@ See [Conventional Commits](Https://conventionalcommits.org) for commit guideline
<!-- changelog -->
## [v0.3.0](https://git.inhji.de/inhji/mirage/compare/v0.2.0...v0.3.0) (2021-02-05)
### Features:
* add markdown helpers
* add editor js
* add migration for html fields
## [v0.2.0](https://git.inhji.de/inhji/mirage/compare/v0.2.0...v0.2.0) (2021-02-05)

59
assets/js/editor.js

@ -0,0 +1,59 @@
import {keymap, EditorView} from "@codemirror/view"
import {EditorState} from "@codemirror/state"
import {history, historyKeymap} from "@codemirror/history"
import {defaultKeymap} from "@codemirror/commands"
import {defaultHighlightStyle} from "@codemirror/highlight"
import {markdown} from "@codemirror/lang-markdown"
const contentElement = "input.editor-content"
let view
export default function initEditor(element) {
content = findContentElement()
if (content === null) {
throw Error(`Content Element not found. Looking for ${contentElement}.`)
}
attachFormHandler()
const parent = document.querySelector(element)
const state = EditorState.create({
doc: content,
extensions: [
history(),
markdown(),
defaultHighlightStyle,
keymap.of([
...defaultKeymap,
...historyKeymap
]),
]
})
console.log(state.doc)
view = new EditorView({ state, parent })
}
function findContentElement() {
const el = document.querySelector(contentElement)
if (el) {
return el.value;
} else {
return null;
}
}
function attachFormHandler() {
const el = document.querySelector(contentElement)
const formEl = el.form
if (el.form) {
el.form.addEventListener("submit", () => {
el.value = view.state.doc.toString()
})
}
}

13
lib/mirage/markdown.ex

@ -0,0 +1,13 @@
import Ecto.Changeset, only: [get_change: 2, put_change: 3]
defmodule Mirage.Markdown do
def maybe_render_markdown(changeset, markdown_field, html_field) do
if markdown = get_change(changeset, markdown_field) do
html = Earmark.as_html!(markdown)
put_change(changeset, html_field, html)
else
changeset
end
end
end

14
priv/repo/migrations/20210205175108_add_html_fields.exs

@ -0,0 +1,14 @@
defmodule Mirage.Repo.Migrations.AddHtmlFields do
use Ecto.Migration
def change do
alter table(:settings) do
modify :value, :text
add :value_html, :text
end
alter table(:notes) do
add :content_html, :text
end
end
end
Loading…
Cancel
Save