-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds MJML support to Keila. Huge thanks to @jdrouet for his Rust implementation of MJML and @paulgoetze for the Elixir wrapper
- Loading branch information
Showing
23 changed files
with
1,583 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const indentAndAutocompleteWithTab = { | ||
key: "Tab", | ||
preventDefault: true, | ||
shift: indentLess, | ||
run: (e) => { | ||
if (!completionStatus(e.state)) return indentMore(e) | ||
return acceptCompletion(e) | ||
} | ||
} | ||
|
||
export const saveUpdates = (source) => { | ||
return EditorView.updateListener.of((e) => { | ||
if (e.docChanged) { | ||
source.value = e.state.doc.toString() | ||
source.dispatchEvent(new Event("change", { bubbles: true })) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { acceptCompletion, completionStatus } from "@codemirror/autocomplete" | ||
import { defaultKeymap, indentLess, indentMore } from "@codemirror/commands" | ||
import { html } from "@codemirror/lang-html" | ||
import { EditorState } from "@codemirror/state" | ||
import { EditorView, keymap } from "@codemirror/view" | ||
import { basicSetup } from "codemirror" | ||
|
||
import tags from "./autocomplete.js" | ||
import { indentAndAutocompleteWithTab, saveUpdates } from "./helpers.js" | ||
import theme from "./theme.js" | ||
|
||
export default class MjmlEditor { | ||
constructor(place, source) { | ||
this.source = source | ||
this.place = place | ||
|
||
let state = EditorState.create({ | ||
doc: source.value, | ||
extensions: [ | ||
basicSetup, | ||
html({ extraTags: tags, selfClosingTags: true }), | ||
keymap.of([...defaultKeymap, indentAndAutocompleteWithTab]), | ||
theme, | ||
saveUpdates(source) | ||
] | ||
}) | ||
|
||
this.view = new EditorView({ | ||
state: state, | ||
parent: place | ||
}) | ||
|
||
place.parentNode.parentNode.addEventListener("x-show-image-dialog", () => { | ||
document | ||
.querySelector("[data-dialog-for=image]") | ||
.dispatchEvent(new CustomEvent("x-show", { detail: {} })) | ||
window.addEventListener( | ||
"update-image", | ||
(e) => { | ||
const { src } = e.detail | ||
if (!src) { | ||
this.view.focus() | ||
return | ||
} | ||
this.view.dispatch(this.view.state.replaceSelection(src)) | ||
}, | ||
{ once: true } | ||
) | ||
}) | ||
} | ||
} |
Oops, something went wrong.