Files
discourse/spec/system/page_objects/cdp.rb
Martin Brennan d3c2bd015d FEATURE: Implement chat transcripts in rich editor (#31819)
This commit allows the ProseMirror rich editor to display chat
transcripts copied from chat using the "Copy" button.

The BBCode usually looks something like this:

```
[chat quote="hunter;29856;2025-03-20T07:13:04Z" channel="design gems 🎉" channelId="95"]
haha **ok** _cool_
[/chat]
```

But there are several variations that must be accounted for:

* Single message from single user
* Multiple messages from a single and multiple users
* Messages inside chat threads

The rich transcript extension has to ignore many of the chat transcript
markdown
tokens because they simply aren't necessary -- none of the ProseMirror
nodes need
to be editable. So, we basically recreate the same HTML that the chat
transcript markdown
rule does in the `toDOM()` function. Maybe in future we want to make the
markdown rule
do less and have this HTML creation in one place, but for now we need to
mirror in both files.

---------

Co-authored-by: Renato Atilio <renato@discourse.org>
2025-03-31 14:04:22 +10:00

93 lines
2.4 KiB
Ruby

# frozen_string_literal: true
module PageObjects
class CDP
include Capybara::DSL
include SystemHelpers
include RSpec::Matchers
def allow_clipboard
cdp_params = {
origin: page.server_url,
permission: {
name: "clipboard-read",
},
setting: "granted",
}
page.driver.browser.execute_cdp("Browser.setPermission", **cdp_params)
cdp_params = {
origin: page.server_url,
permission: {
name: "clipboard-write",
},
setting: "granted",
}
page.driver.browser.execute_cdp("Browser.setPermission", **cdp_params)
end
def read_clipboard
page.evaluate_async_script("navigator.clipboard.readText().then(arguments[0])")
end
def write_clipboard(content, html: false)
if html
page.evaluate_async_script(
"navigator.clipboard.write([
new ClipboardItem({
'text/html': new Blob([arguments[0]], { type: 'text/html' }),
'text/plain': new Blob([arguments[0]], { type: 'text/plain' })
})
]).then(arguments[1])",
content,
)
else
page.evaluate_async_script(
"navigator.clipboard.writeText(arguments[0]).then(arguments[1])",
content,
)
end
end
def clipboard_has_text?(text, chomp: false, strict: true)
try_until_success do
clipboard_text = chomp ? read_clipboard.chomp : read_clipboard
expect(clipboard_text).to strict ? eq(text) : include(text)
end
end
def copy_paste(text, html: false)
allow_clipboard
write_clipboard(text, html: html)
page.send_keys([PLATFORM_KEY_MODIFIER, "v"])
end
def with_network_disconnected
begin
page.driver.browser.network_conditions = { offline: true }
yield
ensure
page.driver.browser.network_conditions = { offline: false }
end
end
def with_slow_download
begin
page.driver.browser.network_conditions = { latency: 20_000, download_throughput: 1 }
yield
ensure
page.driver.browser.network_conditions = { latency: 0 }
end
end
def with_slow_upload
begin
page.driver.browser.network_conditions = { latency: 20_000, upload_throughput: 1 }
yield
ensure
page.driver.browser.network_conditions = { latency: 0 }
end
end
end
end