Replace optional.js with @hedgedoc/optional

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2022-06-23 17:10:41 +02:00
parent 96c0cfa566
commit 3f44283d14
22 changed files with 44 additions and 47 deletions

View file

@ -50,6 +50,7 @@
"@hedgedoc/markdown-it-task-lists": "1.0.3",
"@hedgedoc/realtime": "0.0.5",
"@matejmazur/react-katex": "3.1.3",
"@mrdrogdrog/optional": "0.0.4",
"@react-hook/resize-observer": "1.2.5",
"@redux-devtools/core": "3.13.1",
"@reduxjs/toolkit": "1.8.2",
@ -97,7 +98,6 @@
"markmap-view": "0.2.7",
"mermaid": "9.1.2",
"next": "12.1.6",
"optional-js": "2.3.0",
"react": "18.2.0",
"react-bootstrap": "1.6.5",
"react-bootstrap-typeahead": "5.2.2",

View file

@ -6,7 +6,7 @@
import type { PropsWithChildren } from 'react'
import React, { createContext, useContext, useState } from 'react'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import type { EditorView } from '@codemirror/view'
import type { ContentEdits } from '../editor-pane/tool-bar/formatters/types/changes'
import type { CursorSelection } from '../editor-pane/tool-bar/formatters/types/cursor-selection'

View file

@ -5,8 +5,7 @@
*/
import { useMemo } from 'react'
import Optional from 'optional-js'
import type { CodeMirrorSelection } from './code-mirror-selection'
import { Optional } from '@mrdrogdrog/optional'
import type { ContentFormatter } from './change-content-context'
import { useCodeMirrorReference } from './change-content-context'
import type { CursorSelection } from '../editor-pane/tool-bar/formatters/types/cursor-selection'
@ -45,6 +44,6 @@ export const useChangeEditorContentCallback = () => {
const convertSelectionToCodeMirrorSelection = (selection: CursorSelection | undefined) => {
return Optional.ofNullable(selection)
.map<CodeMirrorSelection | undefined>((selection) => ({ anchor: selection.from, head: selection.to }))
.map((selection) => ({ anchor: selection.from, head: selection.to }))
.orElse(undefined)
}

View file

@ -7,7 +7,7 @@
import { useChangeEditorContentCallback } from '../../change-content-context/use-change-editor-content-callback'
import { useCallback } from 'react'
import type { ContentEdits } from '../../editor-pane/tool-bar/formatters/types/changes'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
const TASK_REGEX = /(\s*(?:[-*+]|\d+[.)]) )(\[[ xX]?])/
@ -23,7 +23,7 @@ export const useSetCheckboxInEditor = () => {
const lines = markdownContent.split('\n')
const lineStartIndex = findStartIndexOfLine(lines, changedLineIndex)
const edits = Optional.ofNullable(TASK_REGEX.exec(lines[changedLineIndex]))
.map<ContentEdits>(([, beforeCheckbox, oldCheckbox]) => {
.map(([, beforeCheckbox, oldCheckbox]) => {
const checkboxStartIndex = lineStartIndex + beforeCheckbox.length
return createCheckboxContentEdit(checkboxStartIndex, oldCheckbox, checkboxChecked)
})

View file

@ -8,7 +8,7 @@ import { useMemo } from 'react'
import { EditorView } from '@codemirror/view'
import type { Extension } from '@codemirror/state'
import { handleUpload } from '../use-handle-upload'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import type { CursorSelection } from '../../tool-bar/formatters/types/cursor-selection'
const calculateCursorPositionInEditor = (view: EditorView, event: MouseEvent): number => {

View file

@ -11,7 +11,7 @@ import { useCallback } from 'react'
import { getGlobalState } from '../../../../../redux'
import { Logger } from '../../../../../utils/logger'
import { findRegexMatchInText } from './find-regex-match-in-text'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import { useHandleUpload } from '../use-handle-upload'
import type { CursorSelection } from '../../tool-bar/formatters/types/cursor-selection'
@ -40,7 +40,7 @@ export const useOnImageUploadFromRenderer = (): void => {
const file = new File([blob], fileName, { type: blob.type })
const { cursorSelection, alt, title } = Optional.ofNullable(lineIndex)
.flatMap((actualLineIndex) => findPlaceholderInMarkdownContent(actualLineIndex, placeholderIndexInLine))
.orElseGet(() => ({}))
.orElse({} as ExtractResult)
handleUpload(file, cursorSelection, alt, title)
})
.catch((error) => log.error(error))

View file

@ -9,7 +9,7 @@ import { EditorView } from '@codemirror/view'
import type { Extension } from '@codemirror/state'
import { useApplicationState } from '../../../../../hooks/common/use-application-state'
import { changeEditorContent } from '../../../change-content-context/use-change-editor-content-callback'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import { replaceSelection } from '../../tool-bar/formatters/replace-selection'
import { convertClipboardTableToMarkdown, isTable } from './table-extractor'
import { isCursorInCodeFence } from './codefenceDetection'

View file

@ -11,7 +11,7 @@ import { ForkAwesomeIcon } from '../../../../common/fork-awesome/fork-awesome-ic
import { EmojiPicker } from './emoji-picker'
import { cypressId } from '../../../../../utils/cypress-attribute'
import type { EmojiClickEventDetail } from 'emoji-picker-element/shared'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import { useChangeEditorContentCallback } from '../../../change-content-context/use-change-editor-content-callback'
import { replaceSelection } from '../formatters/replace-selection'
import { extractEmojiShortCode } from './extract-emoji-short-code'

View file

@ -5,7 +5,7 @@
*/
import type { ContentEdits } from './types/changes'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
export const replaceInContent = (currentContent: string, replaceable: string, replacement: string): ContentEdits => {
return Optional.ofNullable(currentContent.indexOf(replaceable))

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { EditorState } from '@codemirror/state'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
/**
* Extracts the currently selected text from the given CodeMirror state.
@ -16,6 +16,6 @@ export const extractSelectedText = (state: EditorState): string | undefined => {
return Optional.ofNullable(state.selection.main)
.map((selection) => [selection.from, selection.to])
.filter(([from, to]) => from !== to)
.map<string | undefined>(([from, to]) => state.sliceDoc(from, to))
.map(([from, to]) => state.sliceDoc(from, to))
.orElse(undefined)
}

View file

@ -15,7 +15,7 @@ import { useHandleUpload } from '../../hooks/use-handle-upload'
import { ShowIf } from '../../../../common/show-if/show-if'
import { useCodeMirrorReference } from '../../../change-content-context/change-content-context'
import { extractSelectedText } from './extract-selected-text'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
/**
* Shows a button that uploads a chosen file to the backend and adds the link to the note.
@ -33,7 +33,7 @@ export const UploadImageButton: React.FC = () => {
const onUploadImage = useCallback(
(file: File) => {
const description = Optional.ofNullable(codeMirror?.state)
.map<string | undefined>((state) => extractSelectedText(state))
.map((state) => extractSelectedText(state))
.orElse(undefined)
handleUpload(file, undefined, description)
return Promise.resolve()

View file

@ -1,10 +1,10 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import { useContext } from 'react'
import type { HistoryToolbarStateWithDispatcher } from './toolbar-context'
import { historyToolbarStateContext } from './history-toolbar-state-context-provider'

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -7,7 +7,7 @@
import type { Element, Node } from 'domhandler'
import { isTag, isText } from 'domhandler'
import { TravelerNodeProcessor } from '../../node-preprocessors/traveler-node-processor'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import { BlockquoteExtraTagMarkdownExtension } from './blockquote-extra-tag-markdown-extension'
/**

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -10,9 +10,9 @@ import type { Element } from 'domhandler'
import { isText } from 'domhandler'
import { ForkAwesomeIcon } from '../../../common/fork-awesome/fork-awesome-icon'
import { cssColor } from './blockquote-border-color-node-preprocessor'
import Optional from 'optional-js'
import type { Text } from 'domhandler/lib/node'
import { BlockquoteExtraTagMarkdownExtension } from './blockquote-extra-tag-markdown-extension'
import { Optional } from '@mrdrogdrog/optional'
/**
* Replaces <blockquote-tag> elements with "color" as label and a valid color as content
@ -32,7 +32,7 @@ export class BlockquoteColorExtraTagReplacer extends ComponentReplacer {
.filter(isText)
.map((child) => (child as Text).data)
.filter((content) => cssColor.test(content))
.map<NodeReplacement>((color) => (
.map((color) => (
<span className={'blockquote-extra'} key={(index += 1)} style={{ color: color }}>
<ForkAwesomeIcon key='icon' className={'mx-1'} icon={'tag'} />
</span>

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -7,7 +7,7 @@
import type MarkdownIt from 'markdown-it/lib'
import type Token from 'markdown-it/lib/token'
import type { IconName } from '../../../common/fork-awesome/types'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import type StateInline from 'markdown-it/lib/rules_inline/state_inline'
import { BlockquoteExtraTagMarkdownExtension } from './blockquote-extra-tag-markdown-extension'
import type { RuleInline } from 'markdown-it/lib/parser_inline'

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -11,7 +11,7 @@ import type { ForkAwesomeIconProps } from '../../../common/fork-awesome/fork-awe
import { ForkAwesomeIcon } from '../../../common/fork-awesome/fork-awesome-icon'
import type { IconName } from '../../../common/fork-awesome/types'
import { ForkAwesomeIcons } from '../../../common/fork-awesome/fork-awesome-icons'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import type { ReactElement } from 'react'
import { BlockquoteExtraTagMarkdownExtension } from './blockquote-extra-tag-markdown-extension'
@ -44,9 +44,7 @@ export class BlockquoteExtraTagReplacer extends ComponentReplacer {
private buildIconElement(node: Element): ReactElement<ForkAwesomeIconProps> | undefined {
return Optional.ofNullable(node.attribs['data-icon'] as IconName)
.filter((iconName) => ForkAwesomeIcons.includes(iconName))
.map<ReactElement<ForkAwesomeIconProps> | undefined>((iconName) => (
<ForkAwesomeIcon key='icon' className={'mx-1'} icon={iconName} />
))
.map((iconName) => <ForkAwesomeIcon key='icon' className={'mx-1'} icon={iconName} />)
.orElse(undefined)
}
}

View file

@ -6,7 +6,7 @@
import type MarkdownIt from 'markdown-it'
import type { RuleCore } from 'markdown-it/lib/parser_core'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import { parseCodeBlockParameters } from './code-block-parameters'
const ruleName = 'code-highlighter'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import type { LanguageDescription } from '@codemirror/language'
import { parseCodeBlockParameters } from './code-block-parameters'
@ -21,7 +21,7 @@ export const findLanguageByCodeBlockName = (
inputLanguageName: string
): LanguageDescription | null => {
return Optional.ofNullable(parseCodeBlockParameters(inputLanguageName).language)
.map<LanguageDescription | null>((filteredLanguage) =>
.map((filteredLanguage) =>
languages.find((language) => language.name === filteredLanguage || language.alias.includes(filteredLanguage))
)
.orElse(null)

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -11,7 +11,7 @@ import type { ComponentReplacer, NodeReplacement, ValidReactDomElement } from '.
import { DO_NOT_REPLACE, REPLACE_WITH_NOTHING } from '../replace-components/component-replacer'
import React from 'react'
import type { LineWithId } from '../markdown-extension/linemarker/types'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import { LinemarkerMarkdownExtension } from '../markdown-extension/linemarker/linemarker-markdown-extension'
type LineIndexPair = [startLineIndex: number, endLineIndex: number]

View file

@ -1,11 +1,11 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { Logger } from '../../../utils/logger'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
/**
* Error that will be thrown if a message couldn't be sent.

View file

@ -5,7 +5,7 @@
*/
import type { NoteDetails } from '../types/note-details'
import Optional from 'optional-js'
import { Optional } from '@mrdrogdrog/optional'
import { buildStateFromUpdatedMarkdownContentLines } from '../build-state-from-updated-markdown-content'
const TASK_REGEX = /(\s*(?:[-*+]|\d+[.)]) )\[[ xX]?]( .*)/

View file

@ -1993,6 +1993,7 @@ __metadata:
"@hedgedoc/markdown-it-task-lists": 1.0.3
"@hedgedoc/realtime": 0.0.5
"@matejmazur/react-katex": 3.1.3
"@mrdrogdrog/optional": 0.0.4
"@next/bundle-analyzer": 12.1.6
"@react-hook/resize-observer": 1.2.5
"@redux-devtools/core": 3.13.1
@ -2077,7 +2078,6 @@ __metadata:
mermaid: 9.1.2
netlify-cli: 10.5.1
next: 12.1.6
optional-js: 2.3.0
prettier: 2.7.1
react: 18.2.0
react-bootstrap: 1.6.5
@ -2673,6 +2673,13 @@ __metadata:
languageName: node
linkType: hard
"@mrdrogdrog/optional@npm:0.0.4":
version: 0.0.4
resolution: "@mrdrogdrog/optional@npm:0.0.4"
checksum: f23ece2f8e72f8f807a185f739a681661879c8e66137f1ec07764133353b224876e56852215bab79114d4885e5eab1fa4447a3f584c551eb57f832cc7f4e43bf
languageName: node
linkType: hard
"@mrmlnc/readdir-enhanced@npm:^2.2.1":
version: 2.2.1
resolution: "@mrmlnc/readdir-enhanced@npm:2.2.1"
@ -16121,13 +16128,6 @@ __metadata:
languageName: node
linkType: hard
"optional-js@npm:2.3.0":
version: 2.3.0
resolution: "optional-js@npm:2.3.0"
checksum: f957ed47291c23b4fc54cd46df10606f2c8e7784210ee200446900b1bec9dcafc9e80f351e88939a4b5a03d405b0513a8dbdf7a39599d780e6d980015928be01
languageName: node
linkType: hard
"optionator@npm:^0.8.1":
version: 0.8.3
resolution: "optionator@npm:0.8.3"