BookStackApp/BookStack

View on GitHub
resources/js/wysiwyg/lexical/core/LexicalEvents.ts

Summary

Maintainability
F
1 wk
Test Coverage

File LexicalEvents.ts has 1106 lines of code (exceeds 250 allowed). Consider refactoring.
Open

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
Severity: Major
Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 2 days to fix

    Function onBeforeInput has 206 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    function onBeforeInput(event: InputEvent, editor: LexicalEditor): void {
      const inputType = event.inputType;
      const targetRange = getTargetRange(event);
    
      // We let the browser do its own thing for composition.
    Severity: Major
    Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 1 day to fix

      Function onKeyDown has a Cognitive Complexity of 42 (exceeds 5 allowed). Consider refactoring.
      Open

      function onKeyDown(event: KeyboardEvent, editor: LexicalEditor): void {
        lastKeyDownTimeStamp = event.timeStamp;
        lastKeyCode = event.key;
        if (editor.isComposing()) {
          return;
      Severity: Minor
      Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 6 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function onSelectionChange has 112 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      function onSelectionChange(
        domSelection: Selection,
        editor: LexicalEditor,
        isActive: boolean,
      ): void {
      Severity: Major
      Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 4 hrs to fix

        Function onKeyDown has 102 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        function onKeyDown(event: KeyboardEvent, editor: LexicalEditor): void {
          lastKeyDownTimeStamp = event.timeStamp;
          lastKeyCode = event.key;
          if (editor.isComposing()) {
            return;
        Severity: Major
        Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 4 hrs to fix

          Function addRootElementEvents has 89 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          export function addRootElementEvents(
            rootElement: HTMLElement,
            editor: LexicalEditor,
          ): void {
            // We only want to have a single global selectionchange event handler, shared
          Severity: Major
          Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 3 hrs to fix

            Function addRootElementEvents has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
            Open

            export function addRootElementEvents(
              rootElement: HTMLElement,
              editor: LexicalEditor,
            ): void {
              // We only want to have a single global selectionchange event handler, shared
            Severity: Minor
            Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 2 hrs to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Function onInput has 69 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

            function onInput(event: InputEvent, editor: LexicalEditor): void {
              // We don't want the onInput to bubble, in the case of nested editors.
              event.stopPropagation();
              updateEditor(editor, () => {
                const selection = $getSelection();
            Severity: Major
            Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 2 hrs to fix

              Function onDocumentSelectionChange has 50 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

              function onDocumentSelectionChange(event: Event): void {
                const target = event.target as null | Element | Document;
                const targetWindow =
                  target == null
                    ? null
              Severity: Minor
              Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 2 hrs to fix

                Function onClick has 49 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                function onClick(event: PointerEvent, editor: LexicalEditor): void {
                  updateEditor(editor, () => {
                    const selection = $getSelection();
                    const domSelection = getDOMSelection(editor._window);
                    const lastSelection = $getPreviousSelection();
                Severity: Minor
                Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 1 hr to fix

                  Function $shouldPreventDefaultAndInsertText has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
                  Open

                  function $shouldPreventDefaultAndInsertText(
                    selection: RangeSelection,
                    domTargetRange: null | StaticRange,
                    text: string,
                    timeStamp: number,
                  Severity: Minor
                  Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 1 hr to fix

                  Cognitive Complexity

                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                  A method's cognitive complexity is based on a few simple rules:

                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                  • Code is considered more complex for each "break in the linear flow of the code"
                  • Code is considered more complex when "flow breaking structures are nested"

                  Further reading

                  Function $shouldPreventDefaultAndInsertText has 34 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                  function $shouldPreventDefaultAndInsertText(
                    selection: RangeSelection,
                    domTargetRange: null | StaticRange,
                    text: string,
                    timeStamp: number,
                  Severity: Minor
                  Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 1 hr to fix

                    Consider simplifying this complex logical expression.
                    Open

                              if (
                                $isTextNode(node) &&
                                textContentSize !== 0 &&
                                // Exclude empty text nodes at boundaries resulting from user's selection
                                !(
                    Severity: Critical
                    Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 1 hr to fix

                      Consider simplifying this complex logical expression.
                      Open

                              if (
                                anchor.type === 'element' &&
                                anchor.offset === 0 &&
                                selection.isCollapsed() &&
                                !$isRootNode(anchorNode) &&
                      Severity: Critical
                      Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 1 hr to fix

                        Function $onCompositionEndImpl has 32 lines of code (exceeds 25 allowed). Consider refactoring.
                        Open

                        function $onCompositionEndImpl(editor: LexicalEditor, data?: string): void {
                          const compositionKey = editor._compositionKey;
                          $setCompositionKey(null);
                        
                          // Handle termination of composition.
                        Severity: Minor
                        Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 1 hr to fix

                          Function $onCompositionEndImpl has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
                          Open

                          function $onCompositionEndImpl(editor: LexicalEditor, data?: string): void {
                            const compositionKey = editor._compositionKey;
                            $setCompositionKey(null);
                          
                            // Handle termination of composition.
                          Severity: Minor
                          Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 1 hr to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          Function onDocumentSelectionChange has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                          Open

                          function onDocumentSelectionChange(event: Event): void {
                            const target = event.target as null | Element | Document;
                            const targetWindow =
                              target == null
                                ? null
                          Severity: Minor
                          Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 1 hr to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          Function removeRootElementEvents has 27 lines of code (exceeds 25 allowed). Consider refactoring.
                          Open

                          export function removeRootElementEvents(rootElement: HTMLElement): void {
                            const doc = rootElement.ownerDocument;
                            const documentRootElementsCount = rootElementsRegistered.get(doc);
                            invariant(
                              documentRootElementsCount !== undefined,
                          Severity: Minor
                          Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 1 hr to fix

                            Avoid deeply nested control flow statements.
                            Open

                                        if (combinedFormat === 0) {
                                          break;
                                        }
                            Severity: Major
                            Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 45 mins to fix

                              Avoid deeply nested control flow statements.
                              Open

                                          if ($isElementNode(anchorNode)) {
                                            anchorNode.select(0);
                                          } else {
                                            anchorNode.getParentOrThrow().select(0);
                                          }
                              Severity: Major
                              Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 45 mins to fix

                                Avoid deeply nested control flow statements.
                                Open

                                    } else if (!IS_FIREFOX && isSelectAll(key, metaKey, ctrlKey)) {
                                      event.preventDefault();
                                      dispatchCommand(editor, SELECT_ALL_COMMAND, event);
                                    }
                                Severity: Major
                                Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 45 mins to fix

                                  Avoid deeply nested control flow statements.
                                  Open

                                            if (nodeType === DOM_ELEMENT_TYPE || nodeType === DOM_TEXT_TYPE) {
                                              const newSelection = $internalCreateRangeSelection(
                                                lastSelection,
                                                domSelection,
                                                editor,
                                  Severity: Major
                                  Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 45 mins to fix

                                    Avoid deeply nested control flow statements.
                                    Open

                                                if (
                                                  lastNode instanceof ParagraphNode &&
                                                  lastNode.getChildrenSize() === 0
                                                ) {
                                                  selection.format = lastNode.getTextFormat();
                                    Severity: Major
                                    Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 45 mins to fix

                                      Avoid deeply nested control flow statements.
                                      Open

                                            if (isCopy(key, shiftKey, metaKey, ctrlKey)) {
                                              event.preventDefault();
                                              dispatchCommand(editor, COPY_COMMAND, event);
                                            } else if (isCut(key, shiftKey, metaKey, ctrlKey)) {
                                              event.preventDefault();
                                      Severity: Major
                                      Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 45 mins to fix

                                        Consider simplifying this complex logical expression.
                                        Open

                                              if (
                                                // If it has been 30ms since the last keydown, then we should
                                                // apply the empty space heuristic. We can't do this for Safari,
                                                // as the keydown fires after composition start.
                                                event.timeStamp < lastKeyDownTimeStamp + ANDROID_COMPOSITION_LATENCY ||
                                        Severity: Major
                                        Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 40 mins to fix

                                          Consider simplifying this complex logical expression.
                                          Open

                                                  if (
                                                    isPossiblyAndroidKeyPress(event.timeStamp) &&
                                                    editor.isComposing() &&
                                                    isSelectionAnchorSameAsFocus
                                                  ) {
                                          Severity: Major
                                          Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 40 mins to fix

                                            Function markCollapsedSelectionFormat has 5 arguments (exceeds 4 allowed). Consider refactoring.
                                            Open

                                              format: number,
                                              style: string,
                                              offset: number,
                                              key: NodeKey,
                                              timeStamp: number,
                                            Severity: Minor
                                            Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 35 mins to fix

                                              Function $shouldPreventDefaultAndInsertText has 5 arguments (exceeds 4 allowed). Consider refactoring.
                                              Open

                                                selection: RangeSelection,
                                                domTargetRange: null | StaticRange,
                                                text: string,
                                                timeStamp: number,
                                                isBeforeInput: boolean,
                                              Severity: Minor
                                              Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts - About 35 mins to fix

                                                Similar blocks of code found in 2 locations. Consider refactoring.
                                                Open

                                                  } else if (isDeleteForward(key, ctrlKey, shiftKey, altKey, metaKey)) {
                                                    if (isDelete(key)) {
                                                      dispatchCommand(editor, KEY_DELETE_COMMAND, event);
                                                    } else {
                                                      event.preventDefault();
                                                Severity: Minor
                                                Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts and 1 other location - About 35 mins to fix
                                                resources/js/wysiwyg/lexical/core/LexicalEvents.ts on lines 1039..1102

                                                Duplicated Code

                                                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                                Tuning

                                                This issue has a mass of 47.

                                                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                                Refactorings

                                                Further Reading

                                                Similar blocks of code found in 2 locations. Consider refactoring.
                                                Open

                                                  } else if (isDeleteBackward(key, altKey, metaKey, ctrlKey)) {
                                                    if (isBackspace(key)) {
                                                      dispatchCommand(editor, KEY_BACKSPACE_COMMAND, event);
                                                    } else {
                                                      event.preventDefault();
                                                Severity: Minor
                                                Found in resources/js/wysiwyg/lexical/core/LexicalEvents.ts and 1 other location - About 35 mins to fix
                                                resources/js/wysiwyg/lexical/core/LexicalEvents.ts on lines 1048..1102

                                                Duplicated Code

                                                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                                Tuning

                                                This issue has a mass of 47.

                                                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                                Refactorings

                                                Further Reading

                                                There are no issues that match your filters.

                                                Category
                                                Status