@playpilot/tpi 7.0.2 → 7.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/editorial.mount.js +6 -6
- package/dist/link-injections.js +1 -1
- package/dist/mount.js +5 -5
- package/package.json +1 -1
- package/src/lib/explore.ts +2 -0
- package/src/routes/+layout.svelte +6 -0
- package/src/routes/components/Editorial/ManualInjection.svelte +3 -3
- package/src/tests/lib/explore.test.js +14 -1
- package/src/tests/routes/components/Editorial/ManualInjection.test.js +42 -0
package/package.json
CHANGED
package/src/lib/explore.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { mount, unmount } from 'svelte'
|
|
|
2
2
|
import Explore from '../routes/components/Explore/Explore.svelte'
|
|
3
3
|
|
|
4
4
|
export const exploreParentSelector = '[data-playpilot-explore]'
|
|
5
|
+
export const explorePreConsentSelector = '[data-playpilot-pre-consent-explore]'
|
|
5
6
|
|
|
6
7
|
let exploreInsertedComponent: object | null = null
|
|
7
8
|
|
|
@@ -14,6 +15,7 @@ export function insertExplore(): void {
|
|
|
14
15
|
if (!target) return
|
|
15
16
|
|
|
16
17
|
destroyExplore()
|
|
18
|
+
document.querySelector<HTMLElement>(explorePreConsentSelector)?.remove()
|
|
17
19
|
|
|
18
20
|
target.innerHTML = ''
|
|
19
21
|
exploreInsertedComponent = mount(Explore, { target })
|
|
@@ -97,6 +97,12 @@
|
|
|
97
97
|
<li>Anatomy of a Fall</li>
|
|
98
98
|
<li>Killers of the Flower Moon</li>
|
|
99
99
|
</ul>
|
|
100
|
+
|
|
101
|
+
<h2>Example of short manual match</h2>
|
|
102
|
+
<div>
|
|
103
|
+
<p><em>The Testaments</em> er en spændende fortsættelse i universet fra <em>The Handmaid’s Tale</em> og bygger på Margaret Atwoods populære bog. Serien dykker endnu dybere ned i det dystopiske samfund Gilead og følger nye karakterer, der kæmper imod regimet.</p><p><strong>Star Wars: Maul – Shadow Lord</strong></p>
|
|
104
|
+
<p>Denne serie handler om at Maul forsøger at genopbygge sit kriminelle syndikat på en planet, der endnu ikke er under Imperiets kontrol. Her møder han en desillusioneret ung jedi-padawan, som måske kan blive den lærling, han har brug for i sin jagt på hævn.</p>
|
|
105
|
+
</div>
|
|
100
106
|
</article>
|
|
101
107
|
{/key}
|
|
102
108
|
{/if}
|
|
@@ -87,8 +87,9 @@
|
|
|
87
87
|
// Get the node the text is in. If the content of the node is very short we use the parent node instead.
|
|
88
88
|
// This is meant for content that is inside of other elements such as <p>Some <strong>word</strong> in a sentence</p>
|
|
89
89
|
// If we selected "word", we'd still want the full sentence, rather than just the "word".
|
|
90
|
+
// We don't do this if the node is a paragraph, since that implies it is a full sentence by itself at least.
|
|
90
91
|
let node = range.startContainer
|
|
91
|
-
while((node.textContent || '').length <= selectionText.length * 3 && node.parentNode) {
|
|
92
|
+
while((node.textContent || '').length <= selectionText.length * 3 && node.parentNode && node.nodeName !== 'P') {
|
|
92
93
|
// Stop when reaching the parent or any child of the parent that doesn't contain any extra content.
|
|
93
94
|
// This prevents matching on text outside of elements that are top level already.
|
|
94
95
|
if (node.parentNode.textContent === injectionsParent.textContent) break
|
|
@@ -103,9 +104,8 @@
|
|
|
103
104
|
const absoluteStart = getAbsoluteOffset(node, range.startContainer, range.startOffset)
|
|
104
105
|
const absoluteEnd = getAbsoluteOffset(node, range.endContainer, range.endOffset)
|
|
105
106
|
|
|
106
|
-
// Find sentence boundaries
|
|
107
107
|
const before = fullText.slice(0, absoluteStart).lastIndexOf('.')
|
|
108
|
-
const afterMatch = fullText.slice(absoluteEnd).match(/[
|
|
108
|
+
const afterMatch = fullText.slice(absoluteEnd).match(/[.!?\n]/)
|
|
109
109
|
const after = afterMatch ? absoluteEnd + afterMatch.index! : fullText.length
|
|
110
110
|
|
|
111
111
|
const sentenceStart = before === -1 ? 0 : before + 1
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
2
|
-
import { destroyExplore, insertExplore, insertExploreIntoNavigation } from '$lib/explore'
|
|
2
|
+
import { destroyExplore, explorePreConsentSelector, insertExplore, insertExploreIntoNavigation } from '$lib/explore'
|
|
3
3
|
import { mount, unmount } from 'svelte'
|
|
4
4
|
vi.mock('svelte', () => ({
|
|
5
5
|
mount: vi.fn(() => true),
|
|
@@ -36,6 +36,19 @@ describe('explore.js', () => {
|
|
|
36
36
|
|
|
37
37
|
expect(document.querySelector('span')).not.toBeTruthy()
|
|
38
38
|
})
|
|
39
|
+
|
|
40
|
+
it('Should remove pre-consent elements if present', () => {
|
|
41
|
+
document.body.innerHTML = `
|
|
42
|
+
<div data-playpilot-explore><span></span></div>
|
|
43
|
+
<div data-playpilot-pre-consent-explore></div>
|
|
44
|
+
`
|
|
45
|
+
|
|
46
|
+
expect(document.querySelector(explorePreConsentSelector)).toBeTruthy()
|
|
47
|
+
|
|
48
|
+
insertExplore()
|
|
49
|
+
|
|
50
|
+
expect(document.querySelector(explorePreConsentSelector)).not.toBeTruthy()
|
|
51
|
+
})
|
|
39
52
|
})
|
|
40
53
|
|
|
41
54
|
describe('destroyExplore', () => {
|
|
@@ -186,6 +186,48 @@ describe('ManualInjection.svelte', () => {
|
|
|
186
186
|
})
|
|
187
187
|
})
|
|
188
188
|
|
|
189
|
+
it('Should not select parent if content is too short but is in a paragraph', async () => {
|
|
190
|
+
vi.mocked(searchTitles).mockResolvedValueOnce([title])
|
|
191
|
+
vi.mocked(getIndexOfSelection).mockReturnValueOnce({ start: 0, end: 0 })
|
|
192
|
+
|
|
193
|
+
document.body.innerHTML = '<main><p>Some paragraph</p> <p>Some other paragraph</p></main>'
|
|
194
|
+
|
|
195
|
+
const container = document.querySelectorAll('p')[1]
|
|
196
|
+
|
|
197
|
+
// @ts-ignore
|
|
198
|
+
window.getSelection = vi.fn(() => ({
|
|
199
|
+
toString: () => 'Some other paragraph',
|
|
200
|
+
getRangeAt: () => ({
|
|
201
|
+
commonAncestorContainer: container,
|
|
202
|
+
startContainer: container,
|
|
203
|
+
startOffset: 0,
|
|
204
|
+
endOffset: 0,
|
|
205
|
+
cloneContents: () => ({ childNodes: [] }),
|
|
206
|
+
}),
|
|
207
|
+
anchorNode: container,
|
|
208
|
+
focusNode: container,
|
|
209
|
+
}))
|
|
210
|
+
|
|
211
|
+
const onsave = vi.fn()
|
|
212
|
+
const { getByText } = render(ManualInjection, { pageText: document.body.innerText, onsave })
|
|
213
|
+
|
|
214
|
+
await fireEvent.mouseUp(window)
|
|
215
|
+
await fireEvent.click(getByText(title.title))
|
|
216
|
+
await fireEvent.click(getByText('Add playlink'))
|
|
217
|
+
|
|
218
|
+
expect(onsave).toHaveBeenCalledWith({
|
|
219
|
+
sid: title.sid,
|
|
220
|
+
title: 'Some other paragraph',
|
|
221
|
+
sentence: 'Some other paragraph',
|
|
222
|
+
playpilot_url: `https://www.playpilot.com/show/${title.slug}`,
|
|
223
|
+
key: expect.any(String),
|
|
224
|
+
title_details: title,
|
|
225
|
+
manual: true,
|
|
226
|
+
phrase_before: expect.any(String),
|
|
227
|
+
phrase_after: expect.any(String),
|
|
228
|
+
})
|
|
229
|
+
})
|
|
230
|
+
|
|
189
231
|
it('Should not select content if it is outside of given parent', async () => {
|
|
190
232
|
vi.mocked(searchTitles).mockResolvedValueOnce([title])
|
|
191
233
|
vi.mocked(getIndexOfSelection).mockReturnValueOnce({ start: 0, end: 0 })
|