From 7e26a769b9abf3520df59f4d1e3b48dc76e38e48 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 20 Aug 2021 23:41:57 +0100 Subject: [PATCH] clear url hash when selection is cleared --- src/components/EditorControls.js | 1 + src/components/EditorTextArea.js | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/components/EditorControls.js b/src/components/EditorControls.js index dea3f9a..d92a0cc 100644 --- a/src/components/EditorControls.js +++ b/src/components/EditorControls.js @@ -64,6 +64,7 @@ export default function EditorControls({ setLanguage('plain'); history.replace({ pathname: '/', + hash: '', }); document.title = 'paste'; } diff --git a/src/components/EditorTextArea.js b/src/components/EditorTextArea.js index abeb07f..8e6bca3 100644 --- a/src/components/EditorTextArea.js +++ b/src/components/EditorTextArea.js @@ -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