Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
8d8f6e3
Set language of code block
alexwarren Mar 28, 2025
8cacc8b
Fix linting
alexwarren Mar 28, 2025
73ee2d4
Don't need tabindex set
alexwarren Mar 28, 2025
1ff8dab
Merge branch 'main' into awarren/code-block-language-picker
alexwarren Mar 28, 2025
32a3114
Make language indicator clickable
alexwarren Mar 28, 2025
c2bb993
Merge branch 'main' into awarren/code-block-language-picker
alexwarren Apr 4, 2025
05fbf36
Wire it up
alexwarren Apr 4, 2025
b2240c4
Refactor and format
alexwarren Apr 4, 2025
fc83a0e
The linter is stupid
alexwarren Apr 4, 2025
e71bb1b
Focus back on editor when pressing Enter
alexwarren Apr 4, 2025
0c6fed0
Always stop propagation of textbox keys
alexwarren Apr 4, 2025
9a32fca
Populate current language
alexwarren Apr 4, 2025
056e4b8
Prevent ProseMirror freaking out on triple-clicking textbox
alexwarren Apr 4, 2025
e46c971
Language selector needs the same thing
alexwarren Apr 4, 2025
2cb0146
Adding suggestions dropdown
alexwarren Apr 7, 2025
fb25a08
Render dropdown in update method instead
alexwarren Apr 7, 2025
e412cfb
Tweaks
alexwarren Apr 7, 2025
4cfd15d
Merge branch 'main' into awarren/code-block-language-picker
alexwarren Apr 8, 2025
58fd5ee
Don't put "(auto)" in the textbox
alexwarren Apr 8, 2025
874f693
Actually for auto-detected, don't prepopulate it at all
alexwarren Apr 8, 2025
96e35c5
Press Escape to cancel edit, and ignore spaces
alexwarren Apr 8, 2025
d182113
Limit number of suggestions
alexwarren Apr 8, 2025
fa5405c
Lint and tidy
alexwarren Apr 8, 2025
c757e0b
Move language list into options
alexwarren Apr 9, 2025
a2b9719
Format
alexwarren Apr 9, 2025
2c1cf84
Max suggestions from config
alexwarren Apr 9, 2025
73c4482
Implementing design
alexwarren Apr 11, 2025
53b0fde
Merge branch 'main' into awarren/code-block-language-picker
alexwarren Apr 11, 2025
fe0d96f
Move the list inside a container
alexwarren Apr 11, 2025
0e18743
More like design
alexwarren Apr 14, 2025
52b50f7
Highlight menu on hover
alexwarren Apr 14, 2025
24db714
Keyboard navigation
alexwarren Apr 14, 2025
5de5368
Add keyboard shortcut to open dropdown
alexwarren Apr 15, 2025
213f3ed
Updated contrast
alexwarren Apr 16, 2025
446d495
Merge branch 'main' into awarren/code-block-language-picker
alexwarren Apr 16, 2025
4812c6f
Lint
alexwarren Apr 16, 2025
dda97d1
Move code block commands into code-block.ts
alexwarren Apr 16, 2025
6b2669e
Tidy
alexwarren Apr 16, 2025
0eba96f
Tidy
alexwarren Apr 16, 2025
417dda8
Better arrow key handling
alexwarren Apr 16, 2025
03248fa
More tidying
alexwarren Apr 16, 2025
2a29d31
More tidying
alexwarren Apr 17, 2025
71eae1c
Lint
alexwarren Apr 17, 2025
1f27684
Create gold-wombats-jog.md
alexwarren Apr 22, 2025
559ae99
Fix tests
alexwarren Apr 22, 2025
748741c
Add some unit tests
alexwarren Apr 22, 2025
29435ea
Add tests
alexwarren Apr 22, 2025
31156df
Lowercase languages in tests
alexwarren Apr 22, 2025
fc82ec1
Use generic querySelector instead of instanceof
alexwarren Apr 22, 2025
0c6089e
Move the initial codeblock content into the tests
alexwarren Apr 22, 2025
506ca6d
Turn indented blocks into fenced ones when setting language
alexwarren Apr 22, 2025
a460dfc
Better way of turning indented blocks into fenced ones when setting l…
alexwarren Apr 23, 2025
0915b06
Show language list with a popover z-index
alexwarren Apr 23, 2025
6792593
Use .s-btn classes for the button styling
alexwarren Apr 23, 2025
d102803
Add descriptive text to the button
alexwarren Apr 23, 2025
dc60fd6
Button is a button
alexwarren Apr 23, 2025
9aedf6a
Add appropriate id to input, update label
alexwarren Apr 23, 2025
8402837
Add a comment
alexwarren Apr 28, 2025
83592f1
Fix Enter in textbox adding a newline to the editor
alexwarren Apr 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gold-wombats-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackoverflow/stacks-editor": minor
---

add codeblock language picker
9 changes: 9 additions & 0 deletions site/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ domReady(() => {
],
highlighting: {
highlightedNodeTypes: ["stack_snippet_lang"],
languages: [
"javascript",
"java",
"python",
"ruby",
"rust",
"csharp",
"go",
],
},
},
imageUpload: imageUploadOptions,
Expand Down
28 changes: 28 additions & 0 deletions src/rich-text/commands/code-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,31 @@ export function toggleInlineCode(
// If we found neither newline nor softbreak, toggle the inline code mark.
return toggleMark(state.schema.marks.code)(state, dispatch);
}

function isSelectionInCodeBlock(
state: EditorState
): { pos: number; node: ProseMirrorNode } | null {
const { $from } = state.selection;
if ($from.parent.type.name === "code_block") {
return { pos: $from.before(), node: $from.parent };
}
return null;
}

export function openCodeBlockLanguagePicker(
state: EditorState,
dispatch: (tr: Transaction) => void
) {
const codeBlock = isSelectionInCodeBlock(state);
if (!codeBlock) {
return false;
}
const { pos, node } = codeBlock;

// Setting isEditingLanguage to true will open the language picker
const newAttrs = { ...node.attrs, isEditingLanguage: true };
if (dispatch) {
dispatch(state.tr.setNodeMarkup(pos, undefined, newAttrs));
}
return true;
}
18 changes: 16 additions & 2 deletions src/rich-text/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export interface RichTextOptions extends CommonViewOptions {
highlighting?: {
/** Which prosemirror nodes should have highlighting? Defaults to "code_block", which will always be highlighted */
highlightedNodeTypes?: string[];
/** Which languages appear as suggestions in the dropdown? */
languages?: string[];
/** The maximum number of languages to show in the dropdown */
maxSuggestions?: number;
};
}

Expand Down Expand Up @@ -149,8 +153,18 @@ export class RichTextEditor extends BaseView {
],
}),
nodeViews: {
code_block: (node) => {
return new CodeBlockView(node);
code_block: (
node,
view: EditorView,
getPos: () => number
) => {
return new CodeBlockView(
node,
view,
getPos,
this.options.highlighting?.languages || [],
this.options.highlighting?.maxSuggestions
);
},
image(
node: ProseMirrorNode,
Expand Down
2 changes: 2 additions & 0 deletions src/rich-text/key-bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
splitCodeBlockAtStartOfDoc,
exitInclusiveMarkCommand,
escapeUnselectableCommand,
openCodeBlockLanguagePicker,
} from "./commands";

export function allKeymaps(
Expand All @@ -45,6 +46,7 @@ export function allKeymaps(
"Mod-]": indentCodeBlockLinesCommand,
"Mod-[": unindentCodeBlockLinesCommand,
"Enter": splitCodeBlockAtStartOfDoc,
"Mod-;": openCodeBlockLanguagePicker,
});

const tableKeymap = caseNormalizeKeymap({
Expand Down
Loading
Loading