rich editor
This commit is contained in:
@@ -32,7 +32,7 @@ export function htmlurl(channel, file, preset) {
|
|||||||
} else if (file.mime === "image/svg+xml") {
|
} else if (file.mime === "image/svg+xml") {
|
||||||
html = `<img src="${url}" alt="${file.path}"/>`;
|
html = `<img src="${url}" alt="${file.path}"/>`;
|
||||||
} else {
|
} else {
|
||||||
html = `<a href="${url}">${file.originalName}</a>`;
|
html = `<a href="${url}">${file.filename}</a>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return html;
|
return html;
|
||||||
|
|||||||
@@ -8,19 +8,25 @@
|
|||||||
import { indentWithTab } from "@codemirror/commands";
|
import { indentWithTab } from "@codemirror/commands";
|
||||||
import { markdown } from "@codemirror/lang-markdown";
|
import { markdown } from "@codemirror/lang-markdown";
|
||||||
import { lintKeymap } from "@codemirror/lint";
|
import { lintKeymap } from "@codemirror/lint";
|
||||||
|
import { fileurl, presetUrl } from "../files/imageserver";
|
||||||
|
|
||||||
let parentElement;
|
let parentElement;
|
||||||
let codeMirrorView;
|
let codeMirrorView;
|
||||||
export let value;
|
export let value;
|
||||||
export let editable = true;
|
export let editable = true;
|
||||||
|
|
||||||
export function insertMedia(info) {
|
export function insertMedia(channel, files, presetPath) {
|
||||||
let insertText = "";
|
const insertText = files.reduce((text, aFile) => {
|
||||||
if (info.file.width > 0) {
|
const url =
|
||||||
insertText = ``;
|
aFile.width > 0
|
||||||
} else {
|
? presetUrl(channel, aFile, presetPath)
|
||||||
insertText = `[${info.file.filename}](${info.originalUrl})`;
|
: fileurl(channel, aFile);
|
||||||
}
|
|
||||||
|
let addTest = ``;
|
||||||
|
|
||||||
|
return text + "\n" + addTest;
|
||||||
|
}, "");
|
||||||
|
|
||||||
const cursor = codeMirrorView.state.selection.main.head;
|
const cursor = codeMirrorView.state.selection.main.head;
|
||||||
const transaction = codeMirrorView.state.update({
|
const transaction = codeMirrorView.state.update({
|
||||||
changes: {
|
changes: {
|
||||||
|
|||||||
@@ -11,15 +11,16 @@
|
|||||||
value = e.target.value;
|
value = e.target.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function insertMedia(info) {
|
export function insertMedia(html) {
|
||||||
if (info.file.width > 0) {
|
console.log({ html });
|
||||||
var attachment = new Trix.Attachment({ content: info.html });
|
var attachment = new Trix.Attachment({ content: html });
|
||||||
editor.editor.insertAttachment(attachment);
|
editor.editor.insertAttachment(attachment);
|
||||||
} else {
|
// if (info.file.width > 0) {
|
||||||
editor.editor.insertHTML(
|
// var attachment = new Trix.Attachment({ content: html });
|
||||||
`<a href="${info.originalUrl}">${info.file.filename}</a>`,
|
// editor.editor.insertAttachment(attachment);
|
||||||
);
|
// } else {
|
||||||
}
|
// editor.editor.insertHTML(html);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
|||||||
@@ -15,34 +15,14 @@
|
|||||||
$: errorMessage = getErrorMessage(validationErrors, field.name);
|
$: errorMessage = getErrorMessage(validationErrors, field.name);
|
||||||
let editor;
|
let editor;
|
||||||
|
|
||||||
function insertMedia(e) {
|
|
||||||
editor.insertMedia(e.detail);
|
|
||||||
}
|
|
||||||
|
|
||||||
function onFilesInserted(e) {
|
function onFilesInserted(e) {
|
||||||
console.log(e.detail);
|
|
||||||
const presetPath = e.detail.preset.path;
|
const presetPath = e.detail.preset.path;
|
||||||
|
editor.insertMedia(channel, e.detail.files, presetPath);
|
||||||
e.detail.files.map((aFile) => {
|
|
||||||
let html = htmlurl(channel, record, presetPath);
|
|
||||||
|
|
||||||
editor.insertMedia({
|
|
||||||
html: html,
|
|
||||||
url: presetUrl(channel, aFile, presetPath),
|
|
||||||
originalUrl: channel.filesUrl + "/" + aFile.path,
|
|
||||||
file: aFile,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<RichEditorFiles
|
<RichEditorFiles {record} {field} {validationErrors} {onFilesInserted}
|
||||||
{record}
|
|
||||||
{field}
|
|
||||||
{validationErrors}
|
|
||||||
{onFilesInserted}
|
|
||||||
on:editor-insert={insertMedia}
|
|
||||||
></RichEditorFiles>
|
></RichEditorFiles>
|
||||||
<Codemirror
|
<Codemirror
|
||||||
bind:this={editor}
|
bind:this={editor}
|
||||||
|
|||||||
@@ -2,7 +2,10 @@
|
|||||||
import RichEditorFiles from "./RichEditorFiles.svelte";
|
import RichEditorFiles from "./RichEditorFiles.svelte";
|
||||||
import { getErrorMessage } from "./errorMessage";
|
import { getErrorMessage } from "./errorMessage";
|
||||||
import Trix from "../../libs/Trix.svelte";
|
import Trix from "../../libs/Trix.svelte";
|
||||||
|
import { htmlurl } from "../../files/imageserver";
|
||||||
|
import { getContext } from "svelte";
|
||||||
|
|
||||||
|
const channel = getContext("channel");
|
||||||
export let value;
|
export let value;
|
||||||
export let field;
|
export let field;
|
||||||
export let record;
|
export let record;
|
||||||
@@ -12,19 +15,18 @@
|
|||||||
|
|
||||||
$: errorMessage = getErrorMessage(validationErrors, field.name);
|
$: errorMessage = getErrorMessage(validationErrors, field.name);
|
||||||
|
|
||||||
function insertMedia(e) {
|
function onFilesInserted(e) {
|
||||||
editor.insertMedia(e.detail);
|
const presetPath = e.detail.preset.path;
|
||||||
|
e.detail.files.map((aFile) => {
|
||||||
|
const html = htmlurl(channel, aFile, presetPath);
|
||||||
|
editor.insertMedia(html);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="mb-0">
|
<div class="mb-0">
|
||||||
<Trix {field} bind:this={editor} bind:value></Trix>
|
<Trix {field} bind:this={editor} bind:value></Trix>
|
||||||
<RichEditorFiles
|
<RichEditorFiles {record} {field} {onFilesInserted}></RichEditorFiles>
|
||||||
{record}
|
|
||||||
{field}
|
|
||||||
{validationErrors}
|
|
||||||
on:editor-insert={insertMedia}
|
|
||||||
></RichEditorFiles>
|
|
||||||
|
|
||||||
{#if errorMessage}
|
{#if errorMessage}
|
||||||
<div class="invalid-feedback d-block">
|
<div class="invalid-feedback d-block">
|
||||||
|
|||||||
Reference in New Issue
Block a user