Skip to content
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

feat: add text and heading align options #1

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
41,697 changes: 41,697 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

234 changes: 134 additions & 100 deletions src/index.tsx

Large diffs are not rendered by default.

38 changes: 35 additions & 3 deletions src/menus/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {
BoldIcon,
CodeIcon,
Heading1Icon,
Heading2Icon,
ItalicIcon,
BlockQuoteIcon,
LinkIcon,
StrikethroughIcon,
InputIcon,
HighlightIcon,
AlignLeftIcon,
AlignRightIcon,
AlignCenterIcon,
} from "outline-icons";
import { isInTable } from "prosemirror-tables";
import { EditorState } from "prosemirror-state";
Expand All @@ -28,6 +29,16 @@ export default function formattingMenuItems(
const isList = isInList(state);
const allowBlocks = !isTable && !isList;

const isLeftAligned = isNodeActive(schema.nodes.paragraph, {
layoutClass: "left",
});
const isRightAligned = isNodeActive(schema.nodes.paragraph, {
layoutClass: "right",
});
const isCenterAligned = isNodeActive(schema.nodes.paragraph, {
layoutClass: "center",
});

return [
{
name: "link",
Expand Down Expand Up @@ -89,11 +100,32 @@ export default function formattingMenuItems(
attrs: { level: 2 },
visible: allowBlocks,
},
{
name: "separator",
},
{
name: "alignTextLeft",
tooltip: dictionary.alignLeft,
icon: AlignLeftIcon,
active: isLeftAligned,
},
{
name: "alignTextCenter",
tooltip: dictionary.alignCenter,
icon: AlignCenterIcon,
active: isCenterAligned,
},
{
name: "alignTextRight",
tooltip: dictionary.alignRight,
icon: AlignRightIcon,
active: isRightAligned,
},
{
name: "add_flashcard",
tooltip: dictionary.quote,
text: "+ Flashcard",
visible: true
visible: true,
},
];
}
2 changes: 1 addition & 1 deletion src/menus/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function imageMenuItems(
tooltip: dictionary.alignCenter,
icon: AlignImageCenterIcon,
visible: true,
active: state =>
active: (state) =>
isNodeActive(schema.nodes.image)(state) && isCenterAligned(state),
},
{
Expand Down
51 changes: 33 additions & 18 deletions src/nodes/Heading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class Heading extends Node {
get schema() {
return {
attrs: {
layoutClass: { default: null },
level: {
default: 1,
},
Expand All @@ -35,24 +36,13 @@ export default class Heading extends Node {
group: "block",
defining: true,
draggable: false,
parseDOM: this.options.levels.map(level => ({
parseDOM: this.options.levels.map((level) => ({
tag: `h${level}`,
attrs: { level },
getAttrs: (dom) => this.extractLayoutClassFromDOM(dom),
contentElement: "span",
})),
toDOM: node => {
const button = document.createElement("button");
button.innerText = "#";
button.type = "button";
button.className = "heading-anchor";
button.addEventListener("click", this.handleCopyLink());

return [
`h${node.attrs.level + (this.options.offset || 0)}`,
// button,
["span", 0],
];
},
toDOM: (node) => this.createDOMAttributes(node),
};
}

Expand All @@ -78,7 +68,7 @@ export default class Heading extends Node {
}

handleCopyLink = () => {
return event => {
return (event) => {
// this is unfortunate but appears to be the best way to grab the anchor
// as it's added directly to the dom by a decoration.
const anchor = event.currentTarget.parentNode.previousSibling;
Expand Down Expand Up @@ -119,7 +109,7 @@ export default class Heading extends Node {
}

get plugins() {
const getAnchors = doc => {
const getAnchors = (doc) => {
const decorations: Decoration[] = [];
const previouslySeen = {};

Expand Down Expand Up @@ -171,18 +161,43 @@ export default class Heading extends Node {
},
},
props: {
decorations: state => plugin.getState(state),
decorations: (state) => plugin.getState(state),
},
});

return [plugin];
}

inputRules({ type }: { type: NodeType }) {
return this.options.levels.map(level =>
return this.options.levels.map((level) =>
textblockTypeInputRule(new RegExp(`^(#{1,${level}})\\s$`), type, () => ({
level,
}))
);
}

extractLayoutClassFromDOM(dom) {
const className = dom.className;
const layoutClassMatched = className && className.match(/text-(.*)$/);
const layoutClass = layoutClassMatched ? layoutClassMatched[1] : null;
return { layoutClass };
}

createDOMAttributes(node) {
const className = node.attrs.layoutClass
? `text-${node.attrs.layoutClass}`
: null;

const button = document.createElement("button");
button.innerText = "#";
button.type = "button";
button.className = "heading-anchor";
button.addEventListener("click", this.handleCopyLink());

return [
`h${node.attrs.level + (this.options.offset || 0)}`,
// button,
["span", { class: className }, 0],
];
}
}
48 changes: 0 additions & 48 deletions src/nodes/Paragraph.ts

This file was deleted.

91 changes: 91 additions & 0 deletions src/nodes/Paragraph.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { setBlockType } from "prosemirror-commands";
import Node from "./Node";

export default class Paragraph extends Node {
get name() {
return "paragraph";
}

get schema() {
return {
attrs: {
layoutClass: { default: null },
},
content: "inline*",
group: "block",
parseDOM: [
{
tag: "p",
getAttrs: (dom) => this.extractLayoutClassFromDOM(dom),
},
],
toDOM: (node) => this.createDOMAttributes(node),
};
}

keys({ type }) {
return {
"Shift-Ctrl-0": setBlockType(type),
};
}

commands({ type }) {
const alignText = (state, dispatch, layoutClass) => {
const attrs = { layoutClass };
const { selection } = state;
const pos = selection.$from.pos - 1;
const node = state.doc.nodeAt(pos);
if (node) {
const newAttrs = { ...node.attrs, ...attrs };
const tr = state.tr.setNodeMarkup(pos, null, newAttrs);
dispatch(tr);
return true;
}
return false;
};

return {
setBlockType: () => setBlockType(type),
alignTextRight: () => (state, dispatch) =>
alignText(state, dispatch, "right"),
alignTextLeft: () => (state, dispatch) =>
alignText(state, dispatch, "left"),
alignTextCenter: () => (state, dispatch) =>
alignText(state, dispatch, "center"),
};
}

toMarkdown(state, node) {
if (this.isEmptyParagraph(node, state)) {
state.write("\\\n");
} else {
state.renderInline(node);
state.closeBlock(node);
}
}

parseMarkdown() {
return { block: "paragraph" };
}

extractLayoutClassFromDOM(dom) {
const className = dom.className;
const layoutClassMatched = className && className.match(/text-(.*)$/);
const layoutClass = layoutClassMatched ? layoutClassMatched[1] : null;
return { layoutClass };
}

createDOMAttributes(node) {
const className = node.attrs.layoutClass
? `text-${node.attrs.layoutClass}`
: null;

return ["p", { class: className }, 0];
}

isEmptyParagraph(node, state) {
return (
node.textContent.trim() === "" && node.childCount === 0 && !state.inTable
);
}
}
Loading