Skip to content

Commit

Permalink
initial work on highlight extension
Browse files Browse the repository at this point in the history
  • Loading branch information
samlhuillier committed Nov 17, 2024
1 parent a761a22 commit fb7f0a8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@tiptap/extension-bubble-menu": "^2.4.0",
"@tiptap/extension-character-count": "^2.7.2",
"@tiptap/extension-document": "^2.5.0",
"@tiptap/extension-highlight": "^2.9.1",
"@tiptap/extension-link": "^2.2.4",
"@tiptap/extension-paragraph": "^2.5.0",
"@tiptap/extension-table": "^2.4.0",
Expand Down
43 changes: 39 additions & 4 deletions src/components/Editor/EditorManager.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable react/button-has-type */
import React, { useEffect, useState } from 'react'
import { EditorContent, BubbleMenu } from '@tiptap/react'
import { getHTMLFromFragment } from '@tiptap/core'
import { getHTMLFromFragment, Range } from '@tiptap/core'
import TurndownService from 'turndown'
import EditorContextMenu from './EditorContextMenu'
import SearchBar from './Search/SearchBar'
Expand All @@ -17,6 +17,7 @@ const EditorManager: React.FC = () => {
const [showAIPopup, setShowAIPopup] = useState(false)
const { editor } = useFileContext()
const turndownService = new TurndownService()
const [selectedRange, setSelectedRange] = useState<Range | null>(null)

const handleContextMenu = (event: React.MouseEvent<HTMLDivElement>) => {
event.preventDefault()
Expand Down Expand Up @@ -45,6 +46,23 @@ const EditorManager: React.FC = () => {
window.ipcRenderer.on('editor-flex-center-changed', handleEditorChange)
}, [])

useEffect(() => {
if (!editor) return

if (showAIPopup && selectedRange) {
editor.chain().focus().setMark('highlight').run()
}
}, [showAIPopup, selectedRange, editor])

useEffect(() => {
if (!editor) return

if (!showAIPopup) {
editor.chain().focus().unsetMark('highlight').run()
editor.chain().focus().toggleHighlight().run()
}
}, [showAIPopup, editor])

return (
<div
className="relative size-full cursor-text overflow-hidden bg-dark-gray-c-eleven py-4 text-slate-400 opacity-80"
Expand All @@ -61,6 +79,8 @@ const EditorManager: React.FC = () => {
interactiveBorder: 20,
onHidden: () => {
setShowAIPopup(false)
console.log('unsetting selected range')
setSelectedRange(null)
},
maxWidth: 'none',
}}
Expand All @@ -71,6 +91,17 @@ const EditorManager: React.FC = () => {
e.preventDefault()
e.stopPropagation()
}}
onMouseUp={(e) => {
e.preventDefault()
e.stopPropagation()

if (!showAIPopup) {
setSelectedRange({
from: editor.state.selection.from,
to: editor.state.selection.to,
})
}
}}
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
Expand All @@ -80,7 +111,10 @@ const EditorManager: React.FC = () => {
<AiEditMenu
selectedText={turndownService.turndown(
getHTMLFromFragment(
editor.state.doc.slice(editor.state.selection.from, editor.state.selection.to).content,
editor.state.doc.slice(
selectedRange?.from || editor.state.selection.from,
selectedRange?.to || editor.state.selection.to,
).content,
editor.schema,
),
)}
Expand All @@ -89,11 +123,12 @@ const EditorManager: React.FC = () => {
.chain()
.focus()
.deleteRange({
from: editor.state.selection.from,
to: editor.state.selection.to,
from: selectedRange?.from || editor.state.selection.from,
to: selectedRange?.to || editor.state.selection.to,
})
.insertContent(newText)
.run()
setShowAIPopup(false)
}}
/>
) : (
Expand Down
2 changes: 2 additions & 0 deletions src/contexts/FileContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { toast } from 'react-toastify'
import { Markdown } from 'tiptap-markdown'
import { useDebounce } from 'use-debounce'
import { FileInfo, FileInfoTree } from 'electron/main/filesystem/types'
import Highlight from '@tiptap/extension-highlight'
import {
findRelevantDirectoriesToBeExpanded,
flattenFileInfoTree,
Expand Down Expand Up @@ -181,6 +182,7 @@ export const FileProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
transformPastedText: true,
transformCopiedText: false,
}),
Highlight,
TaskItem.configure({
nested: true,
}),
Expand Down
8 changes: 8 additions & 0 deletions src/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ button {
/* Adjust day cell size */
}

/* AI Edit highlight styles */
/* .tiptap .ai-edit-highlight {
background-color: pink;
border-radius: 2px;
padding: 2px 0;
margin: 0 -2px;
} */

@layer base {
:root {
--background: 0 0% 100%;
Expand Down

0 comments on commit fb7f0a8

Please sign in to comment.