Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
47 changes: 30 additions & 17 deletions app/assets/javascript/lexxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4642,6 +4642,7 @@ const global$1 = new Configuration({
attachmentTagName: "action-text-attachment",
attachmentContentTypeNamespace: "actiontext",
authenticatedUploads: false,
remoteImages: true,
extensions: []
});

Expand Down Expand Up @@ -7774,30 +7775,42 @@ class ActionTextAttachmentNode extends ki {
return {
[this.TAG_NAME]: () => {
return {
conversion: (attachment) => ({
node: new ActionTextAttachmentNode({
sgid: attachment.getAttribute("sgid"),
src: attachment.getAttribute("url"),
previewable: attachment.getAttribute("previewable"),
altText: attachment.getAttribute("alt"),
caption: attachment.getAttribute("caption"),
contentType: attachment.getAttribute("content-type"),
fileName: attachment.getAttribute("filename"),
fileSize: attachment.getAttribute("filesize"),
width: attachment.getAttribute("width"),
height: attachment.getAttribute("height")
})
}), priority: 1
conversion: (attachment) => {
const sgid = attachment.getAttribute("sgid");
const contentType = attachment.getAttribute("content-type") || "";
const isRemoteImage = !sgid && contentType.startsWith("image/");

if (isRemoteImage && !Lexxy.global.get("remoteImages")) {
return { node: null }
}

return {
node: new ActionTextAttachmentNode({
sgid,
src: attachment.getAttribute("url"),
previewable: attachment.getAttribute("previewable"),
altText: attachment.getAttribute("alt"),
caption: attachment.getAttribute("caption"),
contentType,
fileName: attachment.getAttribute("filename"),
fileSize: attachment.getAttribute("filesize"),
width: attachment.getAttribute("width"),
height: attachment.getAttribute("height")
})
}
}, priority: 1
}
},
"img": () => {
return {
conversion: (img) => {
if (!Lexxy.global.get("remoteImages")) return { node: null }

const fileName = extractFileName(img.getAttribute("src") ?? "");
return {
node: new ActionTextAttachmentNode({
src: img.getAttribute("src"),
fileName: fileName,
fileName,
caption: img.getAttribute("alt") || "",
contentType: "image/*",
width: img.getAttribute("width"),
Expand All @@ -7817,8 +7830,8 @@ class ActionTextAttachmentNode extends ki {
return {
node: new ActionTextAttachmentNode({
src: videoSource,
fileName: fileName,
contentType: contentType
fileName,
contentType
})
}
}, priority: 1
Expand Down
Binary file modified app/assets/javascript/lexxy.js.br
Binary file not shown.
Binary file modified app/assets/javascript/lexxy.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion app/assets/javascript/lexxy.min.js

Large diffs are not rendered by default.

Binary file modified app/assets/javascript/lexxy.min.js.br
Binary file not shown.
Binary file modified app/assets/javascript/lexxy.min.js.gz
Binary file not shown.
1 change: 1 addition & 0 deletions src/config/lexxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const global = new Configuration({
attachmentTagName: "action-text-attachment",
attachmentContentTypeNamespace: "actiontext",
authenticatedUploads: false,
remoteImages: true,
extensions: []
})

Expand Down
50 changes: 30 additions & 20 deletions src/nodes/action_text_attachment_node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Lexxy from "../config/lexxy"
import { $getEditor, $getNearestRootOrShadowRoot, DecoratorNode, HISTORY_MERGE_TAG } from "lexical"
import { createAttachmentFigure, createElement, isPreviewableImage } from "../helpers/html_helper"
import { bytesToHumanSize } from "../helpers/storage_helper"
import { extractFileName } from "../helpers/storage_helper"

import { bytesToHumanSize, extractFileName } from "../helpers/storage_helper"

export class ActionTextAttachmentNode extends DecoratorNode {
static getType() {
Expand All @@ -22,30 +20,42 @@ export class ActionTextAttachmentNode extends DecoratorNode {
return {
[this.TAG_NAME]: () => {
return {
conversion: (attachment) => ({
node: new ActionTextAttachmentNode({
sgid: attachment.getAttribute("sgid"),
src: attachment.getAttribute("url"),
previewable: attachment.getAttribute("previewable"),
altText: attachment.getAttribute("alt"),
caption: attachment.getAttribute("caption"),
contentType: attachment.getAttribute("content-type"),
fileName: attachment.getAttribute("filename"),
fileSize: attachment.getAttribute("filesize"),
width: attachment.getAttribute("width"),
height: attachment.getAttribute("height")
})
}), priority: 1
conversion: (attachment) => {
const sgid = attachment.getAttribute("sgid")
const contentType = attachment.getAttribute("content-type") || ""
const isRemoteImage = !sgid && contentType.startsWith("image/")

if (isRemoteImage && !Lexxy.global.get("remoteImages")) {
return { node: null }
}

return {
node: new ActionTextAttachmentNode({
sgid,
src: attachment.getAttribute("url"),
previewable: attachment.getAttribute("previewable"),
altText: attachment.getAttribute("alt"),
caption: attachment.getAttribute("caption"),
contentType,
fileName: attachment.getAttribute("filename"),
fileSize: attachment.getAttribute("filesize"),
width: attachment.getAttribute("width"),
height: attachment.getAttribute("height")
})
}
}, priority: 1
}
},
"img": () => {
return {
conversion: (img) => {
if (!Lexxy.global.get("remoteImages")) return { node: null }

const fileName = extractFileName(img.getAttribute("src") ?? "")
return {
node: new ActionTextAttachmentNode({
src: img.getAttribute("src"),
fileName: fileName,
fileName,
caption: img.getAttribute("alt") || "",
contentType: "image/*",
width: img.getAttribute("width"),
Expand All @@ -65,8 +75,8 @@ export class ActionTextAttachmentNode extends DecoratorNode {
return {
node: new ActionTextAttachmentNode({
src: videoSource,
fileName: fileName,
contentType: contentType
fileName,
contentType
})
}
}, priority: 1
Expand Down
103 changes: 103 additions & 0 deletions test/javascript/nodes/action_text_attachment_node.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { expect, test, vi, beforeEach } from "vitest"
import Lexxy from "../../../src/config/lexxy"

vi.mock("lexical", () => ({
$getEditor: () => ({}),
$getNearestRootOrShadowRoot: () => ({}),
DecoratorNode: class {},
HISTORY_MERGE_TAG: "history-merge"
}))

import { ActionTextAttachmentNode } from "../../../src/nodes/action_text_attachment_node"

function createAttachmentElement(attrs = {}) {
const el = document.createElement("action-text-attachment")
for (const [key, value] of Object.entries(attrs)) {
if (value != null) el.setAttribute(key, value)
}
return el
}

beforeEach(() => {
Lexxy.configure({ global: { remoteImages: true } })
})

test("default: importDOM() includes an img handler", () => {
const handlers = ActionTextAttachmentNode.importDOM()
expect(handlers).toHaveProperty("img")
})

test("default: attachment handler converts non-sgid images", () => {
const handlers = ActionTextAttachmentNode.importDOM()
const el = createAttachmentElement({
url: "https://example.com/photo.jpg",
"content-type": "image/jpeg"
})
const result = handlers["action-text-attachment"]().conversion(el)
expect(result.node).not.toBeNull()
expect(result.node.contentType).toBe("image/jpeg")
})

test("remoteImages: false — img handler returns null node", () => {
Lexxy.configure({ global: { remoteImages: false } })
const handlers = ActionTextAttachmentNode.importDOM()
const img = document.createElement("img")
img.setAttribute("src", "https://example.com/photo.jpg")
const result = handlers["img"]().conversion(img)
expect(result.node).toBeNull()
})

test("remoteImages: false — attachment handler returns null for non-sgid image", () => {
Lexxy.configure({ global: { remoteImages: false } })
const handlers = ActionTextAttachmentNode.importDOM()
const el = createAttachmentElement({
url: "https://example.com/photo.jpg",
"content-type": "image/jpeg"
})
const result = handlers["action-text-attachment"]().conversion(el)
expect(result.node).toBeNull()
})

test("remoteImages: false — sgid-backed attachments still import", () => {
Lexxy.configure({ global: { remoteImages: false } })
const handlers = ActionTextAttachmentNode.importDOM()
const el = createAttachmentElement({
sgid: "abc123",
url: "https://example.com/photo.jpg",
"content-type": "image/png",
filename: "photo.png"
})
const result = handlers["action-text-attachment"]().conversion(el)
expect(result.node).not.toBeNull()
expect(result.node.sgid).toBe("abc123")
})

test("remoteImages: false — non-image attachments without sgid still import", () => {
Lexxy.configure({ global: { remoteImages: false } })
const handlers = ActionTextAttachmentNode.importDOM()
const el = createAttachmentElement({
url: "https://example.com/doc.pdf",
"content-type": "application/pdf",
filename: "doc.pdf"
})
const result = handlers["action-text-attachment"]().conversion(el)
expect(result.node).not.toBeNull()
expect(result.node.contentType).toBe("application/pdf")
})

test("remoteImages: false — video handler still present", () => {
Lexxy.configure({ global: { remoteImages: false } })
const handlers = ActionTextAttachmentNode.importDOM()
expect(handlers).toHaveProperty("video")
})

test("config change after importDOM() is respected at conversion time", () => {
const handlers = ActionTextAttachmentNode.importDOM()

const img = document.createElement("img")
img.setAttribute("src", "https://example.com/photo.jpg")
expect(handlers["img"]().conversion(img).node).not.toBeNull()

Lexxy.configure({ global: { remoteImages: false } })
expect(handlers["img"]().conversion(img).node).toBeNull()
})
Loading