clear url hash when selection is cleared

This commit is contained in:
Luck
2021-08-20 23:41:57 +01:00
parent 34655d04bc
commit 7e26a769b9
2 changed files with 8 additions and 2 deletions

View File

@@ -64,6 +64,7 @@ export default function EditorControls({
setLanguage('plain');
history.replace({
pathname: '/',
hash: '',
});
document.title = 'paste';
}

View File

@@ -1,6 +1,7 @@
import { useState, useEffect, useRef } from 'react';
import styled from 'styled-components';
import ReactEditor from 'react-simple-code-editor';
import history from 'history/browser';
import EditorPrismStyle from './EditorPrismStyle';
import { getHighlighter } from '../util/highlighting';
@@ -123,15 +124,19 @@ function useSelectedLine() {
// update window hash when a new line is highlighted
useEffect(() => {
let hash = '';
if (selected[0] !== -1) {
if (selected[1] !== -1) {
const start = Math.min(...selected);
const end = Math.max(...selected);
window.location.hash = `#L${start}-${end}`;
hash = `#L${start}-${end}`;
} else {
window.location.hash = `#L${selected[0]}`;
hash = `#L${selected[0]}`;
}
}
history.replace({ hash });
}, [selected]);
// toggle the highlighting for a given line