diff --git a/CHANGELOG.md b/CHANGELOG.md index 44c45039..2f121e04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # Changelog +## 0.3.4 - 2024-02-06 + +### ✨ Introduce new features + +- Implement clipboard commands + +### 🐛 Fix a bug + +- Add no-cors header to diagram requests +- Hide additional content when not visible +- Make visible by default + +### 📝 Add or update documentation + +- Add visibility toggle to example + ## 0.3.3 - 2024-02-06 ### 🐛 Fix a bug diff --git a/CodeMirror6/CodeMirror6.csproj b/CodeMirror6/CodeMirror6.csproj index ed6ef8d5..209d7d83 100644 --- a/CodeMirror6/CodeMirror6.csproj +++ b/CodeMirror6/CodeMirror6.csproj @@ -9,7 +9,7 @@ GaelJ.BlazorCodeMirror6 true GaelJ.BlazorCodeMirror6 - 0.3.3 + 0.3.4 true snupkg true diff --git a/CodeMirror6/CodeMirror6Wrapper.razor.cs b/CodeMirror6/CodeMirror6Wrapper.razor.cs index df14c68c..92109b80 100644 --- a/CodeMirror6/CodeMirror6Wrapper.razor.cs +++ b/CodeMirror6/CodeMirror6Wrapper.razor.cs @@ -168,7 +168,7 @@ public partial class CodeMirror6Wrapper : ComponentBase /// Whether the editor is visible /// /// - [Parameter] public bool Visible { get; set; } + [Parameter] public bool Visible { get; set; } = true; /// /// Additional attributes to be applied to the container element /// diff --git a/CodeMirror6/CodeMirror6WrapperInternal.razor b/CodeMirror6/CodeMirror6WrapperInternal.razor index 4ae0f0b7..270078f0 100644 --- a/CodeMirror6/CodeMirror6WrapperInternal.razor +++ b/CodeMirror6/CodeMirror6WrapperInternal.razor @@ -1,4 +1,4 @@ -@if (ContentBefore is not null && CmJsInterop is not null && Config is not null && CommandDispatcher is not null) { +@if (Visible && ContentBefore is not null && CmJsInterop is not null && Config is not null && CommandDispatcher is not null) { @ContentBefore((CommandDispatcher, Config, State)) } @if (!IsLoaded) { @@ -22,6 +22,6 @@ -@if (ContentAfter is not null && CmJsInterop is not null && Config is not null && CommandDispatcher is not null) { +@if (Visible && ContentAfter is not null && CmJsInterop is not null && Config is not null && CommandDispatcher is not null) { @ContentAfter((CommandDispatcher, Config, State)) } diff --git a/CodeMirror6/CodeMirror6WrapperInternal.razor.cs b/CodeMirror6/CodeMirror6WrapperInternal.razor.cs index 7af6efd0..f6a4dfbb 100644 --- a/CodeMirror6/CodeMirror6WrapperInternal.razor.cs +++ b/CodeMirror6/CodeMirror6WrapperInternal.razor.cs @@ -169,7 +169,7 @@ public partial class CodeMirror6WrapperInternal : ComponentBase, IAsyncDisposabl /// Whether the editor is visible /// /// - [Parameter] public bool Visible { get; set; } + [Parameter] public bool Visible { get; set; } = true; /// /// Additional attributes to be applied to the container element /// diff --git a/CodeMirror6/Models/CodeMirrorSimpleCommand.cs b/CodeMirror6/Models/CodeMirrorSimpleCommand.cs index e9f099e5..634a2f23 100644 --- a/CodeMirror6/Models/CodeMirrorSimpleCommand.cs +++ b/CodeMirror6/Models/CodeMirrorSimpleCommand.cs @@ -177,4 +177,19 @@ public enum CodeMirrorSimpleCommand /// Focus the CodeMirror editor /// Focus, + + /// + /// Cut the current selection + /// + Cut, + + /// + /// Copy the current selection + /// + Copy, + + /// + /// Paste the current selection + /// + Paste, } diff --git a/CodeMirror6/NodeLib/src/CmCommands.ts b/CodeMirror6/NodeLib/src/CmCommands.ts index cae94b0c..16c3c441 100644 --- a/CodeMirror6/NodeLib/src/CmCommands.ts +++ b/CodeMirror6/NodeLib/src/CmCommands.ts @@ -203,10 +203,10 @@ export const toggleMarkdownQuote: Command = (view: EditorView) => toggleCharacte export function toggleMarkdownHeading(headingLevel: number): Command { return (view: EditorView) => toggleCharactersAtStartOfLines(view, "#".repeat(headingLevel), false) } -export const increaseMarkdownHeadingLevel: Command = (view: EditorView) => { +export const increaseMarkdownHeadingLevel: Command = (view: EditorView) => { return modifyHeaderLevelAtSelections(view, -1); } -export const decreaseMarkdownHeadingLevel: Command = (view: EditorView) => { +export const decreaseMarkdownHeadingLevel: Command = (view: EditorView) => { return modifyHeaderLevelAtSelections(view, 1); } export const toggleMarkdownUnorderedList: Command = (view: EditorView) => toggleCharactersAtStartOfLines(view, "-", true) @@ -249,3 +249,38 @@ export function insertTextAboveCommand(view: EditorView, textToInsert: string) { view.state.update(changeSpec, { scrollIntoView: true, annotations: Transaction.userEvent.of('input'), }) ) } + +export async function copy(view: EditorView) { + try { + const text = view.state.sliceDoc(view.state.selection.main.from, view.state.selection.main.to); + if (text === null || text === undefined || text === "") return; + await navigator.clipboard.writeText(text) + view.focus() + return true + } catch (err) { + console.error('Failed to copy text: ', err); + return false; + } +} + +export async function cut(view: EditorView) { + if (await copy(view)) + view.dispatch(view.state.update({ + changes: { from: view.state.selection.main.from, to: view.state.selection.main.to, insert: "" }, + scrollIntoView: true + })); +} + +export async function paste(view: EditorView): Promise { + try { + const text = await navigator.clipboard.readText(); + view.dispatch(view.state.update({ + changes: { from: view.state.selection.main.from, to: view.state.selection.main.to, insert: text }, + scrollIntoView: true + })); + return true; + } catch (err) { + console.error('Failed to paste text: ', err); + return false; + } +} diff --git a/CodeMirror6/NodeLib/src/CmDiagrams.ts b/CodeMirror6/NodeLib/src/CmDiagrams.ts index d6710030..de55d65a 100644 --- a/CodeMirror6/NodeLib/src/CmDiagrams.ts +++ b/CodeMirror6/NodeLib/src/CmDiagrams.ts @@ -70,6 +70,7 @@ async function fetchDiagramSvg(view: EditorView, code: string, language: string, headers: { 'Content-Type': 'text/plain', 'Accept': 'image/svg+xml', + 'mode': 'no-cors', }, body: code }) diff --git a/CodeMirror6/NodeLib/src/index.ts b/CodeMirror6/NodeLib/src/index.ts index c57e67b2..34d5a7df 100644 --- a/CodeMirror6/NodeLib/src/index.ts +++ b/CodeMirror6/NodeLib/src/index.ts @@ -37,6 +37,9 @@ import { decreaseMarkdownHeadingLevel, insertTableAboveCommand, insertHorizontalRuleAboveCommand, + cut, + copy, + paste, } from "./CmCommands" import { dynamicImagesExtension } from "./CmImages" import { externalLintSource, getExternalLinterConfig } from "./CmLint" @@ -444,6 +447,10 @@ export function dispatchCommand(id: string, functionName: string, ...args: any[] case 'InsertTable': insertTableAboveCommand(view, args[0] as number, args[1] as number); break; case 'InsertMarkdownHorizontalRule': insertHorizontalRuleAboveCommand(view); break; + case 'Cut': cut(view); break; + case 'Copy': copy(view); break; + case 'Paste': paste(view); break; + case 'Focus': break; default: throw new Error(`Function ${functionName} does not exist.`); diff --git a/Examples.BlazorServer/Examples.BlazorServer.csproj b/Examples.BlazorServer/Examples.BlazorServer.csproj index 59a3d2a0..ed48b5ae 100644 --- a/Examples.BlazorServer/Examples.BlazorServer.csproj +++ b/Examples.BlazorServer/Examples.BlazorServer.csproj @@ -4,7 +4,7 @@ enable false enable - 0.3.3 + 0.3.4 diff --git a/Examples.BlazorServerInteractive/Examples.BlazorServerInteractive.csproj b/Examples.BlazorServerInteractive/Examples.BlazorServerInteractive.csproj index 50ae27e1..310f32a8 100644 --- a/Examples.BlazorServerInteractive/Examples.BlazorServerInteractive.csproj +++ b/Examples.BlazorServerInteractive/Examples.BlazorServerInteractive.csproj @@ -4,7 +4,7 @@ enable enable false - 0.3.3 + 0.3.4 diff --git a/Examples.BlazorWasm/Examples.BlazorWasm.csproj b/Examples.BlazorWasm/Examples.BlazorWasm.csproj index dd21547f..d300dcc5 100644 --- a/Examples.BlazorWasm/Examples.BlazorWasm.csproj +++ b/Examples.BlazorWasm/Examples.BlazorWasm.csproj @@ -4,7 +4,7 @@ enable enable false - 0.3.3 + 0.3.4 diff --git a/Examples.Common/Example.razor b/Examples.Common/Example.razor index 7b1184fe..308d6884 100644 --- a/Examples.Common/Example.razor +++ b/Examples.Common/Example.razor @@ -1,4 +1,4 @@ -

Code Mirror 6 Wrapper Demo

+

Code Mirror 6 Wrapper Demo