-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat(editor): add format button to codeblock toolbar #9076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
giuxtaposition
wants to merge
26
commits into
TriliumNext:main
Choose a base branch
from
giuxtaposition:feat/add-format-codeblock-button
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
108d8bc
feat(editor): add format button to codeblock toolbar
giuxtaposition 0c6482a
refactor(editor): add formatter registry to support multiple formatte…
giuxtaposition aa09d0c
feat(editor): make PrettierParserConfig plugins type stricter
giuxtaposition fb49152
fix(editor): remove unnecessary trim in extract code text
giuxtaposition 24af153
fix(editor): use vi.waitFor instead of setTimeout in format codeblock…
giuxtaposition ad5ca36
fix(editor): remove XML from supported languages for prettier as it i…
giuxtaposition 1b638fd
fix(editor): gemini suggestion adding double join
giuxtaposition d64c6b3
fix(editor): fix swapped parameter order in onNotificationWarning cal…
giuxtaposition 20fa962
feat(editor): show notification toast when code formatting fails
giuxtaposition 09fd1bd
test(editor): fix format codeblock tests not actually running when ru…
giuxtaposition 64f921a
Merge branch 'main' into feat/add-format-codeblock-button
eliandoran f111468
Merge branch 'main' into feat/add-format-codeblock-button
giuxtaposition 1dcb400
test(ckeditor5): fix templates translation test
giuxtaposition 16fe432
refactor(ckeditor5): move actual code formatting logic in the client …
giuxtaposition e5d7245
Merge branch 'main' into feat/add-format-codeblock-button
giuxtaposition 2a81162
refactor(ckeditor5): move code formatting execution to client
giuxtaposition ced8142
fix(ckeditor5): strip trailing newline from formatted output to preve…
giuxtaposition 93a5536
fix(ckeditor5): add re-entrance guard to prevent concurrent format op…
giuxtaposition d9a2ab5
refactor(ckeditor5): extract CodeFormatterConfig type and remove redu…
giuxtaposition f369f16
fix(editor): simplify onNotificationWarning to not rely on empty-stri…
giuxtaposition 48fb69e
test(editor): add FormatterRegistry.format() tests and remove duplica…
giuxtaposition 6d49adc
refactor(editor): simplify PrettierFormatter error handling and use p…
giuxtaposition 813c7a5
docs: add section about format code block
giuxtaposition 0db3bb2
Merge branch 'main' into feat/add-format-codeblock-button
giuxtaposition 6cb611d
Merge branch 'main' into feat/add-format-codeblock-button
giuxtaposition 5c7f502
Merge remote-tracking branch 'origin/main' into feat/add-format-codeb…
giuxtaposition File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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
This file contains hidden or 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
29 changes: 29 additions & 0 deletions
29
packages/ckeditor5/src/plugins/format_codeblock/code_formatter.ts
This file contains hidden or 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,29 @@ | ||
| export interface CodeFormatter { | ||
| readonly name: string; | ||
| canFormat(language: string): boolean; | ||
| format(code: string, language: string): Promise<string>; | ||
| } | ||
|
|
||
| export class FormatterRegistry { | ||
| private static instance: FormatterRegistry | null = null; | ||
| private readonly formatters: CodeFormatter[] = []; | ||
|
|
||
| static getInstance(): FormatterRegistry { | ||
| if (!FormatterRegistry.instance) { | ||
| FormatterRegistry.instance = new FormatterRegistry(); | ||
| } | ||
| return FormatterRegistry.instance; | ||
| } | ||
|
|
||
| register(formatter: CodeFormatter): void { | ||
| this.formatters.push(formatter); | ||
| } | ||
|
|
||
| getFormatterForLanguage(language: string): CodeFormatter | undefined { | ||
| return this.formatters.find((f) => f.canFormat(language)); | ||
| } | ||
|
|
||
| isLanguageSupported(language: string): boolean { | ||
| return this.formatters.some((f) => f.canFormat(language)); | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
packages/ckeditor5/src/plugins/format_codeblock/format_codeblock_button.ts
This file contains hidden or 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,42 @@ | ||
| import { ButtonView, Notification, Plugin } from "ckeditor5"; | ||
| import formatIcon from "../../icons/format-codeblock.svg?raw"; | ||
| import { FormatterRegistry } from "./code_formatter"; | ||
| import { FormatCodeblockCommand } from "./format_codeblock_command"; | ||
| import { PrettierFormatter } from "./prettier_formatter"; | ||
|
|
||
| export default class FormatCodeblockButton extends Plugin { | ||
| static get requires() { | ||
| return [Notification]; | ||
| } | ||
|
|
||
| public init() { | ||
| const editor = this.editor; | ||
|
|
||
| const registry = FormatterRegistry.getInstance(); | ||
| registry.register(new PrettierFormatter()); | ||
|
giuxtaposition marked this conversation as resolved.
Outdated
|
||
|
|
||
| editor.commands.add( | ||
| "formatCodeblock", | ||
| new FormatCodeblockCommand(this.editor), | ||
| ); | ||
|
|
||
| const componentFactory = editor.ui.componentFactory; | ||
| componentFactory.add("formatCodeblock", (locale) => { | ||
| const button = new ButtonView(locale); | ||
| const command = editor.commands.get("formatCodeblock")!; | ||
|
|
||
| button.set({ | ||
| tooltip: "Format code block", | ||
| icon: formatIcon, | ||
| }); | ||
|
|
||
| button.bind("isEnabled").to(command, "isEnabled"); | ||
|
|
||
| this.listenTo(button, "execute", () => { | ||
| editor.execute("formatCodeblock"); | ||
| }); | ||
|
|
||
| return button; | ||
| }); | ||
| } | ||
| } | ||
97 changes: 97 additions & 0 deletions
97
packages/ckeditor5/src/plugins/format_codeblock/format_codeblock_command.ts
This file contains hidden or 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,97 @@ | ||
| import { Command, ModelElement, Notification } from "ckeditor5"; | ||
| import { FormatterRegistry } from "./code_formatter"; | ||
|
|
||
| export class FormatCodeblockCommand extends Command { | ||
| declare value: string | false; | ||
|
|
||
| override refresh() { | ||
| const codeBlockCommand = this.editor.commands.get("codeBlock"); | ||
| const language = codeBlockCommand?.value; | ||
| const registry = FormatterRegistry.getInstance(); | ||
|
|
||
| if ( | ||
| typeof language === "string" && | ||
| registry.isLanguageSupported(language) | ||
| ) { | ||
| this.isEnabled = true; | ||
| this.value = language; | ||
| } else { | ||
| this.isEnabled = false; | ||
| this.value = false; | ||
| } | ||
| } | ||
|
|
||
| override execute() { | ||
| const editor = this.editor; | ||
| const model = editor.model; | ||
| const selection = model.document.selection; | ||
| const notification = editor.plugins.get(Notification); | ||
| const t = editor.locale.t; | ||
|
|
||
| const language = this.value; | ||
| if (!language) { | ||
| return; | ||
| } | ||
|
|
||
| const registry = FormatterRegistry.getInstance(); | ||
| const formatter = registry.getFormatterForLanguage(language); | ||
| if (!formatter) { | ||
| return; | ||
| } | ||
|
|
||
| const codeBlockEl = selection | ||
| .getFirstPosition() | ||
| ?.findAncestor("codeBlock"); | ||
| if (!codeBlockEl) { | ||
| notification.showWarning( | ||
| t("Unable to find code block element to format."), | ||
| { | ||
| namespace: "formatCodeblock", | ||
| }, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const codeText = this.extractCodeText(codeBlockEl); | ||
|
|
||
| if (!codeText) { | ||
| return; | ||
| } | ||
|
|
||
| formatter | ||
| .format(codeText, language) | ||
| .then((formatted) => { | ||
| if (formatted === codeText) { | ||
| return; | ||
| } | ||
|
|
||
| model.change((writer) => { | ||
| const range = writer.createRangeIn(codeBlockEl); | ||
| writer.remove(range); | ||
| const lines = formatted.split("\n"); | ||
| for (let i = 0; i < lines.length; i++) { | ||
| if (i > 0) { | ||
| writer.appendElement("softBreak", codeBlockEl); | ||
| } | ||
| if (lines[i]) { | ||
| writer.appendText(lines[i], codeBlockEl); | ||
| } | ||
| } | ||
| }); | ||
| }) | ||
| .catch((err) => { | ||
| notification.showWarning(err.message || String(err), { | ||
| title: t( | ||
| `Failed to format code block with ${formatter.name}`, | ||
| ), | ||
| namespace: "formatCodeblock", | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| private extractCodeText(codeBlockEl: ModelElement): string { | ||
| return Array.from(codeBlockEl.getChildren()) | ||
| .map((child) => ("data" in child ? child.data : "\n")) | ||
| .join(""); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
packages/ckeditor5/src/plugins/format_codeblock/languages.ts
This file contains hidden or 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,17 @@ | ||
| export const LANG_JAVASCRIPT_FRONTEND = "application-javascript-env-frontend"; | ||
| export const LANG_JAVASCRIPT_BACKEND = "application-javascript-env-backend"; | ||
| export const LANG_TYPESCRIPT = "application-typescript"; | ||
| export const LANG_TYPESCRIPT_JSX = "text-typescript-jsx"; | ||
| export const LANG_JSX = "text-jsx"; | ||
| export const LANG_JSON = "application-json"; | ||
| export const LANG_YAML = "text-x-yaml"; | ||
| export const LANG_GRAPHQL = "text-x-graphql"; | ||
| export const LANG_HTML = "text-html"; | ||
| export const LANG_XML = "text-xml"; | ||
| export const LANG_MARKDOWN = "text-x-markdown"; | ||
| export const LANG_CSS = "text-css"; | ||
| export const LANG_LESS = "text-x-less"; | ||
| export const LANG_SCSS = "text-x-scss"; | ||
| export const LANG_PYTHON = "text-x-python"; | ||
| export const LANG_RUST = "text-x-rustsrc"; | ||
| export const LANG_C = "text-x-csrc"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.