{"version":3,"file":"attachment_manager-CbRmv5si.js","sources":["../../../app/javascript/controllers/compose/models/attachment_manager.js"],"sourcesContent":["class AttachmentManager {\n constructor(controller) {\n this.inputController = controller\n }\n\n insert(attachment) {\n const pieceAtCursor = this.cursor.pieceAtCaret()\n const pieceBeforeCursor = this.cursor.pieceBeforeCaret()\n\n if(this.inputController.empty) {\n return this.resetContentAndInsertAttachment(attachment)\n }\n\n if(pieceAtCursor.endsWithWhitespace || pieceBeforeCursor.endsWithWhitespace) {\n return this.moveCursorToRightAndInsertAttachment(attachment)\n }\n\n if(pieceAtCursor.isLineFeed && !pieceBeforeCursor) {\n return this.insertAttachment(attachment)\n }\n\n if(pieceAtCursor.string === pieceBeforeCursor.string || (pieceAtCursor.isLineFeed && pieceBeforeCursor)) {\n const characterAtCursor = this.cursor.characterAtCaret()\n const characterBeforeCursor = this.cursor.characterBeforeCaret()\n\n // Example: word |another\n if(characterBeforeCursor.isWhitespace) {\n return this.moveCursorToLeftAndInsertAttachmentAndMoveBackToRight(attachment)\n }\n\n // Example: word| another\n if(characterAtCursor.isWhitespace) {\n return this.insertWhitespaceAndAttachmentAndMoveToRight(attachment)\n }\n\n if(characterAtCursor.isZeroWidthNonJoinerSpace) {\n return this.removeZeroWidthNonJoinerSpaceAndInsertAttachment(attachment)\n }\n\n // Example word|\n if(characterAtCursor.isLineFeed) {\n return this.moveCursorToRightAndInsertAttachmentAndMoveToRight(attachment)\n }\n\n // Example wo|rd\n if(!characterBeforeCursor.isWhitespace && !characterAtCursor.isWhitespace) {\n this.insertWhitespaceTwiceAndThenAttachment(attachment)\n }\n }\n }\n\n resetContentAndInsertAttachment(attachment) {\n this.inputController.nextTick(() => {\n this.inputController.resetContent()\n\n this.recordUndoEntryAndInsertAttachment(attachment)\n\n this.afterAttachmentInsertion()\n })\n }\n\n moveCursorToRightAndInsertAttachment(attachment) {\n const characterBeforeCursor = this.cursor.characterBeforeCaret()\n\n // example word|\n if(!characterBeforeCursor.isWhitespace) {\n this.cursor.moveToRight()\n }\n\n this.inputController.nextTick(() => {\n this.recordUndoEntryAndInsertAttachment(attachment)\n })\n\n return this.afterAttachmentInsertion()\n }\n\n insertAttachment(attachment) {\n this.inputController.nextTick(() => {\n this.recordUndoEntryAndInsertAttachment(attachment)\n })\n\n return this.afterAttachmentInsertion()\n }\n\n moveCursorToLeftAndInsertAttachmentAndMoveBackToRight(attachment) {\n this.inputController.isParsing = true\n\n this.cursor.moveToLeft()\n\n this.inputController.nextTick(() => {\n this.inputController.insertWhitespace()\n\n this.recordUndoEntryAndInsertAttachment(attachment)\n\n this.inputController.isParsing = false\n\n this.afterAttachmentInsertion()\n })\n }\n\n insertWhitespaceAndAttachmentAndMoveToRight(attachment) {\n this.inputController.isParsing = true\n\n this.inputController.nextTick(() => {\n this.inputController.insertWhitespace()\n\n this.recordUndoEntryAndInsertAttachment(attachment)\n\n this.cursor.moveToRight()\n this.inputController.isParsing = false\n })\n\n this.afterAttachmentInsertion()\n }\n\n removeZeroWidthNonJoinerSpaceAndInsertAttachment(attachment) {\n this.inputController.isParsing = true\n\n this.editor.setSelectedRange([0, 0])\n this.editor.deleteInDirection(\"forward\")\n\n this.inputController.nextTick(() => {\n this.recordUndoEntryAndInsertAttachment(attachment)\n\n this.cursor.moveToRight()\n this.inputController.isParsing = false\n })\n\n this.afterAttachmentInsertion()\n }\n\n moveCursorToRightAndInsertAttachmentAndMoveToRight(attachment) {\n this.inputController.isParsing = true\n\n this.inputController.nextTick(() => {\n this.inputController.insertWhitespace()\n this.cursor.moveToRight()\n\n this.inputController.nextTick(() => {\n this.recordUndoEntryAndInsertAttachment(attachment)\n\n this.cursor.moveToRight()\n this.inputController.isParsing = false\n\n return this.afterAttachmentInsertion()\n })\n })\n }\n\n insertWhitespaceTwiceAndThenAttachment(attachment) {\n this.inputController.isParsing = true\n\n this.inputController.nextTick(() => {\n this.splitWordAtCursor()\n this.cursor.moveToLeft()\n\n this.inputController.nextTick(() => {\n this.recordUndoEntryAndInsertAttachment(attachment)\n this.inputController.isParsing = false\n\n return this.afterAttachmentInsertion()\n })\n })\n }\n\n afterAttachmentInsertion() {\n this.inputController.nextTick(() => {\n const characterAfterCursor = this.cursor.characterAfterCaret()\n const characterAtCursor = this.cursor.characterAtCaret()\n\n if(characterAtCursor.isWhitespace && characterAfterCursor.isWhitespace) {\n this.cursor.moveToRight()\n }\n\n if(characterAtCursor.isLineFeed || characterAfterCursor.isLineFeed) {\n this.inputController.isParsing = true\n\n this.inputController.insertWhitespace()\n this.cursor.moveToRight()\n\n this.inputController.isParsing = false\n }\n\n this.inputController.afterAttachmentInsertion()\n })\n }\n\n deleteAttachmentInPosition() {\n // When the cursor is next to a shortlink. Pressing backspace only selects the shortlink since it is an attachment\n // We want to delete the shortlink instead of selecting it.\n\n const [start, end] = this.editor.getSelectedRange()\n\n this.editor.setSelectedRange([start - 1, end])\n this.editor.deleteInDirection('backward')\n\n if(this.trixDocument.toString().length === 2) {\n const characterAtCursor = this.cursor.characterAtCaret()\n const characterAfterCursor = this.cursor.characterAfterCaret()\n\n if(characterAtCursor.isZeroWidthNonJoinerSpace && characterAfterCursor.isLineFeed) {\n this.editor.setSelectedRange([0, 0])\n this.editor.deleteInDirection('forward')\n this.cursor.moveToRight()\n }\n }\n }\n\n splitWordAtCursor() {\n // Transforms wo|rd to wo |rd\n this.inputController.insertWhitespace()\n this.inputController.insertWhitespace()\n }\n\n recordUndoEntryAndInsertAttachment(attachment) {\n this.editor.recordUndoEntry('insertAttachment')\n this.editor.insertAttachment(attachment)\n }\n\n get cursor() {\n return this.inputController.cursor\n }\n\n get editor() {\n return this.inputController.editor\n }\n\n get trixDocument() {\n return this.editor.getDocument()\n }\n\n get oldPosition() {\n return this.inputController.oldPosition\n }\n\n get trixPosition() {\n return this.inputController.trixPosition\n }\n}\n\nexport { AttachmentManager }\n"],"names":["AttachmentManager","controller","attachment","pieceAtCursor","pieceBeforeCursor","characterAtCursor","characterBeforeCursor","characterAfterCursor","start","end"],"mappings":"AAAA,MAAM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