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 option to disable editors #8

Merged
merged 3 commits into from
Nov 15, 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
5 changes: 5 additions & 0 deletions .changeset/wicked-rings-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@guardian/prosemirror-editor": minor
---

Add option to disable editors
Comment on lines +1 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify I understand the release process, it looks from the documentation that this file gets generated automatically by running yarn changeset. Then the changesets GitHub Action will automatically open a release PR which I assume will convert the changeset info into a changelog? (example here)

Copy link
Collaborator Author

@rhystmills rhystmills Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - I was prompted to run yarn changeset by Changeset Bot after my first commit, changesets said there wasn't a 'changeset' and this added one, which I added in a follow-up commit.

4 changes: 4 additions & 0 deletions src/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
padding: 10px;
}

.ProseMirror[contenteditable="false"]{
background-color: #f6f6f6;
cursor: not-allowed
}

.ProseMirror p {
margin: 0px
Expand Down
5 changes: 3 additions & 2 deletions src/ts/RichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ interface RichTextEditorProps {
config: EditorConfig;
label?: string;
shouldAcceptCopiedText?: boolean;
disabled: boolean;
}

export const RichTextEditor = ({ value, onUpdate, config, label, shouldAcceptCopiedText = false }: RichTextEditorProps) => {
export const RichTextEditor = ({ value, onUpdate, config, label, shouldAcceptCopiedText = false, disabled = false }: RichTextEditorProps) => {
const schema = createSchema(config);

const editorEl = useRef<HTMLDivElement>(null);
Expand All @@ -33,7 +34,7 @@ export const RichTextEditor = ({ value, onUpdate, config, label, shouldAcceptCop
// Editor view takes an HTML Node therefore this string value needs to be converted into a node by placing in a div
const contentNode = document.createElement('div');
contentNode.innerHTML = value;
const edView = createEditorView(localOnUpdate, editorEl, contentNode, schema, config);
const edView = createEditorView(localOnUpdate, editorEl, contentNode, schema, config, disabled);
setEditorView(edView);
}, []);

Expand Down
14 changes: 9 additions & 5 deletions src/ts/richtext/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ import { buildMenuItems } from './menu';
import { baseKeymap } from 'prosemirror-commands';
import { EditorConfig } from './config';

const createBasePlugins = (schema: Schema, config: EditorConfig) => {
const createBasePlugins = (schema: Schema, config: EditorConfig, disabled: boolean) => {
const plugins = [
keymap(buildKeymap(schema, {}, {}, config)),
keymap(baseKeymap),
history({ depth: 100, newGroupDelay: 500 }),
menuBar({content: buildMenuItems(schema)})
];
return plugins;
return disabled ? [] : plugins;
};

export const createEditorView = (
onChange: (str: string) => void,
editorEl: RefObject<HTMLDivElement>,
contentEl: HTMLDivElement,
schema: Schema,
config: EditorConfig
config: EditorConfig,
disabled: boolean
) => {
if (!editorEl.current) {
return;
Expand All @@ -35,7 +36,7 @@ export const createEditorView = (
doc: DOMParser.fromSchema(schema).parse(contentEl, {
preserveWhitespace: true
}),
plugins: createBasePlugins(schema, config)
plugins: createBasePlugins(schema, config, disabled)
}),
dispatchTransaction: (transaction: Transaction) => {
const { state, transactions } = ed.state.applyTransaction(transaction);
Expand All @@ -49,7 +50,10 @@ export const createEditorView = (
tmp.appendChild(outputHtml);
onChange(tmp.innerHTML);
}
}
},
editable: () => {
return !disabled;
},
});
return ed;
};