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

Highlight Entity #284

Merged
merged 29 commits into from
Nov 28, 2022
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d515a07
Add basic highlight API
xypnox Nov 17, 2022
61f2ae7
Add types and highlightStoreConstructor v2
xypnox Nov 17, 2022
9153e2d
Fix store calls for adding, removing highlights
xypnox Nov 17, 2022
2e0ebfc
Use 2 as new version, retain old one for checking usage pattern
xypnox Nov 17, 2022
7e49a4b
Add highlight apis to background script
xypnox Nov 17, 2022
f1537f1
Change highlight structure, use store
xypnox Nov 18, 2022
a79fbff
Add determining which blocks to show highlight on
xypnox Nov 18, 2022
ff2119e
Fix delete highlight call (add workspace headers)
xypnox Nov 18, 2022
26861cf
Use highlightStore2, change flow to new highlights
xypnox Nov 18, 2022
f5672e8
Fix calls for addHighlight, split content
xypnox Nov 19, 2022
c3554e5
Fix highlight of url call from new store
xypnox Nov 19, 2022
7570133
Fix highlight store sync
xypnox Nov 19, 2022
996d667
Fix interfacing with highlighter, check before getting highlightmap
xypnox Nov 21, 2022
145f1d3
Highlight sourceUrl in blocks and serialization fixes
xypnox Nov 21, 2022
329690f
Show notes when edit/navigating to highlight
xypnox Nov 22, 2022
3610d8d
Fix deletion of a highlight and its content from notes
xypnox Nov 23, 2022
e91f27e
Add a link if it doesn't exist on highlight
xypnox Nov 23, 2022
25a8133
Fix styles for note links with highlights
xypnox Nov 23, 2022
0fe9ef4
Simplify saveIt
xypnox Nov 24, 2022
ce13d02
Fix analysis worker crash due to missing lic in captured
xypnox Nov 24, 2022
16c1885
Remove old stores and types
xypnox Nov 25, 2022
e3ad035
Remove old store and types
xypnox Nov 26, 2022
a94c128
Add delete blockid requests
xypnox Nov 26, 2022
167db0f
Convert to API class
xypnox Nov 26, 2022
34228d2
Fix sidebar styling
xypnox Nov 28, 2022
a5546ab
Fix fetching highlights
xypnox Nov 28, 2022
e5a1fc1
Fix drag handle animation of extension sidebar
xypnox Nov 28, 2022
42f6791
Remove old code and comments
xypnox Nov 28, 2022
b85994f
Changeset added
Nov 28, 2022
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
Prev Previous commit
Next Next commit
Simplify saveIt
  • Loading branch information
xypnox committed Nov 26, 2022
commit 0fe9ef432ec456614585791e732ca2d1db86b627
185 changes: 108 additions & 77 deletions apps/extension/src/Hooks/useSaveChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import {
extractMetadata,
generateHighlightId,
getHighlightBlockMap,
Highlight,
HighlightBlockMap,
ILink,
mog,
SEPARATOR
NodeProperties,
SEPARATOR,
SingleNamespace
} from '@mexit/core'

import { useContentStore } from '../Stores/useContentStore'
Expand Down Expand Up @@ -52,122 +56,149 @@ export function useSaveChanges() {
const { getDefaultNamespace, getNamespaceOfNodeid } = useNamespaces()
const { saveHighlight } = useHighlights()

/**
* Save
*/
const saveIt = async (saveAndExit = false, notification = false) => {
mog('saveIt', { saveAndExit, notification })
// mog('saveIt', { saveAndExit, notification })
setVisualState(VisualState.animatingOut)

const node = useSputlitStore.getState().node
const namespace = getNamespaceOfNodeid(node?.nodeid) ?? getDefaultNamespace()

const selection = useSputlitStore.getState().selection

// Editor Id is different from nodeId
// const editorId = getPlateId()
const editorState = useEditorStore.getState().nodeContent

mog('nodeContent', editorState)
// mog('nodeContent', editorState)

const parentILink = getParentILink(node.path)
const isRoot = node.path.split(SEPARATOR).length === 1
const isSingle = !!parentILink || isRoot

// const metadata = { saveableRange: selection?.range, sourceUrl: selection?.range && window.location.href }

let request
if (parentILink || isRoot) {
if (!isSharedNode(node.nodeid)) {
updateSingleILink(node.nodeid, node.path, namespace.id)
}

dispatch('ADD_SINGLE_ILINK', node.nodeid, node.path, namespace.id)
dispatchLinkUpdates(node, namespace, isSingle)

request = {
type: 'CAPTURE_HANDLER',
subType: 'SAVE_NODE',
data: {
id: node.nodeid,
title: node.title,
content: editorState,
referenceID: parentILink?.nodeid,
workspaceID: workspaceDetails.id,
namespaceID: namespace.id
}
}
} else {
const linksToBeCreated = getEntirePathILinks(node.path, node.nodeid, namespace.id)
updateMultipleILinks(linksToBeCreated)
dispatch('ADD_MULTIPLE_ILINKS', linksToBeCreated)
request = {
type: 'CAPTURE_HANDLER',
subType: 'BULK_CREATE_NODES',
data: {
path: createNoteHierarchyString(node.path, namespace.id),
id: node.nodeid,
title: node.title,
content: editorState,
workspaceID: workspaceDetails.id,
namespaceID: namespace.id
}
const request = {
type: 'CAPTURE_HANDLER',
subType: isSingle ? 'SAVE_NODE' : 'BULK_CREATE_NODES',
data: {
path: isSingle ? undefined : createNoteHierarchyString(node.path, namespace.id),
id: node.nodeid,
title: node.title,
content: editorState,
referenceID: isSingle ? undefined : parentILink?.nodeid,
workspaceID: workspaceDetails.id,
namespaceID: namespace.id,
highlightId: undefined
}
}

mog('request', { request })

setContent(node.nodeid, editorState)

setSelection(undefined)
addRecent(node.nodeid)
setActiveItem()

// const metadata = { saveableRange: selection?.range, sourceUrl: selection?.range && window.location.href }
const isCapturedHighlight = selection?.range && window.location.href
const highlight = isCapturedHighlight && {
entityId: generateHighlightId(),
properties: {
sourceUrl: selection?.range && window.location.href,
saveableRange: selection?.range
}
}
const { highlight, blockHighlightMap } = await createHighlightEntityFromSelection(
selection,
node.nodeid,
editorState
)
if (highlight) {
// Save highlight
const sourceTitle = document.title
await saveHighlight(highlight, sourceTitle)
request.data.highlightId = highlight.entityId
}

mog('Request and things', { request, node, editorState, highlight })
chrome.runtime.sendMessage(request, (response) => {
const { message, error } = response

mog('Response', { response })
// mog('Response', { response })

if (error && notification) {
toast.error('An Error Occured. Please try again.')
} else {
const bulkCreateRequest = request.subType === 'BULK_CREATE_NODES'

const nodeid = !bulkCreateRequest ? message.id : message.node.id
const content = deserializeContent(!bulkCreateRequest ? message.data : message.node.data)
const metadata = extractMetadata(!bulkCreateRequest ? message : message.node)

dispatch('ADD_RECENT_NODE', nodeid)
mog('DispatchAfterSave', { response, nodeid, content, metadata, highlight, blockHighlightMap })
dispatchAfterSave({ nodeid, content, metadata, highlight, blockHighlightMap }, saveAndExit, notification)
}
})
}

dispatch('SET_CONTENT', nodeid, content, metadata)
/**
* Creates highlight entity from selection
* Does not add the highlight to the store as that requires content to generate blockmap
*
* @param selection
* @returns Highlight
*/
const createHighlightEntityFromSelection = async (selection: any, nodeid: string, content: any[]) => {
const isCapturedHighlight = selection?.range && window.location.href
const highlight = isCapturedHighlight && {
entityId: generateHighlightId(),
properties: {
sourceUrl: selection?.range && window.location.href,
saveableRange: selection?.range
}
}
if (highlight) {
// Save highlight
const sourceTitle = document.title
await saveHighlight(highlight, sourceTitle)
// Extract the blockids for which we have captured highlights
const blockHighlightMap = getHighlightBlockMap(nodeid, content)
// Add highlight in local store and nodeblockmap
addHighlight(highlight, blockHighlightMap)
return { highlight, blockHighlightMap }
}
return { highlight: undefined, blockHighlightMap: undefined }
}

// Create highlight in local store and add to node, block map, replace metadata from block
// Extract the blockids for which we have captured highlights
const blockHighlightMap = getHighlightBlockMap(nodeid, content)
// Add highlight to local store
addHighlight(highlight, blockHighlightMap)
// addHighlightedBlock(nodeid, content)
dispatch('ADD_HIGHLIGHTED_BLOCK', highlight, blockHighlightMap)
const dispatchLinkUpdates = (node: NodeProperties, namespace: SingleNamespace, isSingle: boolean) => {
if (isSingle) {
if (!isSharedNode(node.nodeid)) {
updateSingleILink(node.nodeid, node.path, namespace.id)
}
dispatch('ADD_SINGLE_ILINK', node.nodeid, node.path, namespace.id)
} else {
const linksToBeCreated = getEntirePathILinks(node.path, node.nodeid, namespace.id)
// Why is shared check not used here?
updateMultipleILinks(linksToBeCreated)
dispatch('ADD_MULTIPLE_ILINKS', linksToBeCreated)
}
}

if (notification) {
toast.success('Saved to Cloud')
}
interface DispatchData {
nodeid: string
content: any[]
metadata: any
highlight?: Highlight
blockHighlightMap?: {
nodeId: string
blockIds: string[]
}
}
// Dispatches new items to the stores
const dispatchAfterSave = (
{ nodeid, content, metadata, highlight, blockHighlightMap }: DispatchData,
saveAndExit = false,
notification = false
) => {
dispatch('ADD_RECENT_NODE', nodeid)
dispatch('SET_CONTENT', nodeid, content, metadata)
if (highlight) dispatch('ADD_HIGHLIGHTED_BLOCK', highlight, blockHighlightMap)

if (notification) {
toast.success('Saved to Cloud')
}

if (saveAndExit) {
setVisualState(VisualState.animatingOut)
// So that sputlit opens with preview true when it opens the next time
setPreviewMode(true)
}
}
})
if (saveAndExit) {
setVisualState(VisualState.animatingOut)
// So that sputlit opens with preview true when it opens the next time
setPreviewMode(true)
}
}

const appendAndSave = ({ nodeid, content: toAppendContent, saveAndExit, notification }: AppendAndSaveProps) => {
Expand Down