Skip to content

Commit 2b90f9d

Browse files
shakyShaneShane Osbourne
andauthored
ntp: support window->window drop in favorites (#1260)
* ntp: support window->window drop in favorites * favor the known format * prefer `DDG_MIME_TYPE` --------- Co-authored-by: Shane Osbourne <[email protected]>
1 parent dfaf48d commit 2b90f9d

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

special-pages/pages/new-tab/app/favorites/components/PragmaticDND.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,8 @@ function useGridState(favorites, itemsDidReOrder, instanceId) {
5252
monitorForExternal({
5353
onDrop(payload) {
5454
// const data = '<meta name="application/vnd.duckduckgo.bookmark-by-id" content="3" />';
55-
const data = getHTML(payload);
56-
if (!data) return console.warn('missing text/html payload');
57-
58-
// Create a document fragment using the safer createContextualFragment
59-
const fragment = document.createRange().createContextualFragment(data);
60-
61-
// Get the first element
62-
const node = fragment.firstElementChild;
63-
if (!node) return console.warn('missing first element');
64-
65-
// check the name attribute
66-
if (node.getAttribute('name') !== DDG_MIME_TYPE) return console.warn(`attribute name was not ${DDG_MIME_TYPE}`);
67-
68-
// check the id
69-
const id = node.getAttribute('content');
70-
if (!id) return console.warn('id missing from `content` attribute');
55+
const id = idFromPayload(payload);
56+
if (!id) return;
7157

7258
const location = payload.location;
7359
const target = location.current.dropTargets[0];
@@ -290,3 +276,33 @@ export function useItemState(url, id) {
290276
function getInstanceId() {
291277
return Symbol('instance-id');
292278
}
279+
280+
/**
281+
* @import {ContainsSource} from "@atlaskit/pragmatic-drag-and-drop/dist/types/public-utils/external/native-types.js"
282+
* @param {ContainsSource} payload
283+
*/
284+
function idFromPayload(payload) {
285+
// return the external DDG type first
286+
const ddg = payload.source.getStringData(DDG_MIME_TYPE);
287+
if (ddg && ddg.length > 0) return ddg;
288+
289+
// now try and parse the HTML, which might be `<meta name="application/vnd.duckduckgo.bookmark-by-id" content="3" />`
290+
const html = getHTML(payload);
291+
if (!html) return console.warn(`missing text/html payload + missing ${DDG_MIME_TYPE} mime type`);
292+
293+
// Create a document fragment using the safer createContextualFragment
294+
const fragment = document.createRange().createContextualFragment(html);
295+
296+
// Get the first element
297+
const node = fragment.firstElementChild;
298+
if (!node) return console.warn('missing first element');
299+
300+
// check the name attribute
301+
if (node.getAttribute('name') !== DDG_MIME_TYPE) return console.warn(`attribute name was not ${DDG_MIME_TYPE}`);
302+
303+
// check the id
304+
const id = node.getAttribute('content');
305+
if (!id) return console.warn('id missing from `content` attribute');
306+
307+
return id;
308+
}

0 commit comments

Comments
 (0)