Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
feat: Add 'scrollTo' prop
Browse files Browse the repository at this point in the history
  • Loading branch information
tommoor committed Jul 17, 2020
1 parent 790c700 commit ad22dc5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ A React component that will be wrapped around items that have an optional toolti

A number that will offset the document headings by a number of levels. For example, if you already nest the editor under a main `h1` title you might want the user to only be able to create `h2` headings and below, in this case you would set the prop to `1`.

#### `scrollTo`

A string representing a heading anchor – the document will smooth scroll so that the heading is visible
in the viewport.

#### `embeds`

Optionally define embeds which will be inserted in place of links when the `matcher` function returns a truthy value. The matcher method's return value will be available on the component under `props.attrs.matches`. If `title` and `icon` are provided then the embed will also appear in the block menu.
Expand Down
1 change: 1 addition & 0 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Example extends React.Component {
readOnlyWriteCheckboxes
value={this.state.value}
defaultValue={defaultValue}
scrollTo={window.location.hash}
handleDOMEvents={{
focus: () => console.log("FOCUS"),
blur: () => console.log("BLUR"),
Expand Down
13 changes: 10 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export type Props = {
dark?: boolean;
theme?: typeof theme;
headingsOffset?: number;
scrollTo?: string;
handleDOMEvents?: {
[name: string]: (view: EditorView, event: Event) => boolean;
};
Expand Down Expand Up @@ -155,7 +156,10 @@ class RichMarkdownEditor extends React.PureComponent<Props, State> {

componentDidMount() {
this.init();
this.scrollToAnchor();

if (this.props.scrollTo) {
this.scrollToAnchor(this.props.scrollTo);
}

if (this.props.readOnly) return;

Expand All @@ -179,6 +183,10 @@ class RichMarkdownEditor extends React.PureComponent<Props, State> {
});
}

if (this.props.scrollTo && this.props.scrollTo !== prevProps.scrollTo) {
this.scrollToAnchor(this.props.scrollTo);
}

// Focus at the end of the document if switching from readOnly and autoFocus
// is set to true
if (prevProps.readOnly && !this.props.readOnly && this.props.autoFocus) {
Expand Down Expand Up @@ -416,8 +424,7 @@ class RichMarkdownEditor extends React.PureComponent<Props, State> {
return view;
}

scrollToAnchor() {
const { hash } = window.location;
scrollToAnchor(hash: string) {
if (!hash) return;

try {
Expand Down

0 comments on commit ad22dc5

Please sign in to comment.