codemirror insert

This commit is contained in:
2024-09-07 15:31:56 +03:00
parent 0cd4e08716
commit 02f8f5970a
8 changed files with 66 additions and 8 deletions
+27 -4
View File
@@ -1,10 +1,10 @@
<script>
// https://codesandbox.io/s/codemirror-remark-editor-4m4z9?file=/src/CodeEditor.js:374-387
import {onMount, onDestroy} from "svelte";
import {onDestroy, onMount} from "svelte";
import {basicSetup, EditorView} from "codemirror";
import { autocompletion, completionKeymap } from "@codemirror/autocomplete";
import {EditorState, Compartment} from "@codemirror/state";
import {autocompletion, completionKeymap} from "@codemirror/autocomplete";
import {Compartment, EditorState} from "@codemirror/state";
import {keymap} from "@codemirror/view";
import {indentWithTab} from "@codemirror/commands";
import {markdown} from "@codemirror/lang-markdown";
@@ -15,6 +15,30 @@
export let value;
export let editable = true;
export function insertMedia(info) {
let insertText = "";
if (info.record._file.width > 0) {
insertText = `![${info.record._file.originalName}](${info.url})`;
} else {
insertText = `[${info.record._file.originalName}](${info.originalUrl})`;
}
const cursor = codeMirrorView.state.selection.main.head;
const transaction = codeMirrorView.state.update({
changes: {
from: cursor,
insert: insertText,
},
// the next 2 lines will set the appropriate cursor position after inserting the new text.
selection: {anchor: cursor + 1},
scrollIntoView: true,
});
if (transaction) {
codeMirrorView.dispatch(transaction);
}
}
onMount(() => {
let language = new Compartment();
let tabSize = new Compartment();
@@ -51,7 +75,6 @@
});
});
onDestroy(() => {
+10
View File
@@ -98,6 +98,16 @@
bind:graph
{record}
/>
{:else if field.info.name === "markdown"}
<Markdown
bind:value={data[field.name]}
{schema}
{field}
{validationErrors}
{isCreateMode}
bind:graph
{record}
/>
{:else}
<svelte:component
this={formElement}
@@ -1,21 +1,37 @@
<script>
import Codemirror from "../../libs/CodemirrorMarkdown.svelte";
import { getErrorMessage } from "./errorMessage";
import RichEditorFiles from "./RichEditorFiles.svelte";
export let value;
export let field;
export let graph;
export let record;
export let isCreateMode;
// export let id;
export let validationErrors;
$: errorMessage = getErrorMessage(validationErrors, field.name);
let editor;
function insertMedia(e){
editor.insertMedia(e.detail)
}
</script>
<div class="mb-3">
<Codemirror bind:value editable={!field.readonly || isCreateMode} />
<Codemirror bind:this={editor} bind:value editable={!field.readonly || isCreateMode} />
{#if field.collections.length > 0}
<RichEditorFiles
bind:graph
{record}
{field}
{validationErrors}
on:editor-insert={insertMedia}
>
</RichEditorFiles>
{/if}
{#if errorMessage}
<div class="invalid-feedback d-block">
{errorMessage}
+2 -1
View File
@@ -27,7 +27,6 @@ class RecordController extends Controller
public function __construct(
private readonly RecordService $recordService,
private readonly AccountService $accountService,
private readonly AuthService $authService,
private readonly ChannelService $channelService,
private readonly Svelte $svelte,
private readonly Query $query,
@@ -61,10 +60,12 @@ class RecordController extends Controller
], $filter);
$skip = data_get($urlParams, "skip") ?? 0;
$limit = 30;
$records = [];
$graphArray = null;
$graph = $this->query
->filter($arguments)
->notLinked($request->input("notlinked") ?? "")
+1
View File
@@ -4,6 +4,7 @@ namespace Lucent\Query;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use Lucent\Database\Database;
use Lucent\Query\BuilderConverter\BuilderConverter;
use Lucent\Query\Filter\AndFilter;
+3
View File
@@ -78,6 +78,7 @@ final class Query
// ->toArray();
$formattedRecords = $this->formatRecords($resultsRecordsUnique, $resultChildrenEdges, $resultParentEdges);
$this->reset();
return $formattedRecords;
@@ -94,7 +95,9 @@ final class Query
$queryRecords = collect($records)->map(function ($recordData) {
$record = Record::fromDB($recordData);
$record->data = $this->inputFormatter->fill($record->schema, $record->data);
$queryRecord = QueryRecord::fromRecord($record);
$queryRecord->isRoot = data_get($recordData, "isRoot") === true;
return $queryRecord;
+3
View File
@@ -16,8 +16,11 @@ class InputFormatter
public function fill(string $schemaName, RecordData $input): RecordData
{
$schema = $this->channelService->getSchema($schemaName)->get();
$data = $schema->fields->reduce(fn(array $carry, FieldInterface $field) => $field->format($input->toArray(), $carry), []);
return new RecordData($data);
}
+1
View File
@@ -18,6 +18,7 @@ class Markdown implements FieldInterface, RequiredInterface
public bool $required = false,
public bool $nullable = false,
public string $default = "",
public array $collections = [],
public string $help = "",
public ?int $min = null,
public ?int $max = null,