Skip to content

Commit

Permalink
Revert "refactor: add head element util inject function and use it (#894
Browse files Browse the repository at this point in the history
)" (#899)

This reverts commit e0c17af.
  • Loading branch information
davidlj95 authored Oct 8, 2024
1 parent ab24d2e commit 73f64b4
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 28 deletions.
3 changes: 0 additions & 3 deletions projects/ngx-meta/api-extractor/ngx-meta.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ export const _HEAD_ELEMENT_UPSERT_OR_REMOVE: InjectionToken<_HeadElementUpsertOr
// @internal (undocumented)
export type _HeadElementUpsertOrRemove = (selector: string, element: HTMLElement | null | undefined) => void;

// @internal (undocumented)
export const _injectHeadElementUpsertOrRemove: () => _HeadElementUpsertOrRemove;

// @internal (undocumented)
export const _injectMetadataManagers: () => ReadonlyArray<NgxMetaMetadataManager>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { TestBed } from '@angular/core/testing'
import { HeadElementHarness } from './__tests__/head-element-harness'
import { DOCUMENT } from '@angular/common'
import {
_HEAD_ELEMENT_UPSERT_OR_REMOVE,
_HeadElementUpsertOrRemove,
_injectHeadElementUpsertOrRemove,
} from './head-element-upsert-or-remove'

describe('Head element upsert or remove', () => {
Expand Down Expand Up @@ -95,5 +95,5 @@ describe('Head element upsert or remove', () => {

function makeSut() {
TestBed.configureTestingModule({})
return TestBed.runInInjectionContext(_injectHeadElementUpsertOrRemove)
return TestBed.inject(_HEAD_ELEMENT_UPSERT_OR_REMOVE)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,3 @@ export type _HeadElementUpsertOrRemove = (
selector: string,
element: HTMLElement | null | undefined,
) => void

/**
* @internal
*/
export const _injectHeadElementUpsertOrRemove = () =>
inject(_HEAD_ELEMENT_UPSERT_OR_REMOVE)
1 change: 0 additions & 1 deletion projects/ngx-meta/src/core/src/head-elements/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export {
_HEAD_ELEMENT_UPSERT_OR_REMOVE,
_HeadElementUpsertOrRemove,
_injectHeadElementUpsertOrRemove,
} from './head-element-upsert-or-remove'
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { DOCUMENT } from '@angular/common'
import {
_injectHeadElementUpsertOrRemove,
_HEAD_ELEMENT_UPSERT_OR_REMOVE,
_HeadElementUpsertOrRemove,
_isDefined,
makeMetadataManagerProviderFromSetterFactory,
MetadataSetterFactory,
} from '@davidlj95/ngx-meta/core'
import { JsonLdMetadata } from './json-ld-metadata'
import { DOCUMENT } from '@angular/common'
import { inject } from '@angular/core'

const KEY = 'jsonLd' satisfies keyof JsonLdMetadata
const SCRIPT_TYPE = 'application/ld+json'

export const JSON_LD_METADATA_SETTER_FACTORY: MetadataSetterFactory<
JsonLdMetadata[typeof KEY]
> = () => {
const doc = inject(DOCUMENT)
const headElementUpsertOrRemove = _injectHeadElementUpsertOrRemove()
return (jsonLd) => {
> =
(headElementUpsertOrRemove: _HeadElementUpsertOrRemove, doc: Document) =>
(jsonLd) => {
let scriptElement: HTMLScriptElement | undefined
if (_isDefined(jsonLd)) {
scriptElement = doc.createElement('script')
Expand All @@ -25,7 +24,6 @@ export const JSON_LD_METADATA_SETTER_FACTORY: MetadataSetterFactory<
}
headElementUpsertOrRemove(`script[type='${SCRIPT_TYPE}']`, scriptElement)
}
}

/**
* Manages the {@link JsonLdMetadata.jsonLd} metadata
Expand All @@ -35,6 +33,7 @@ export const JSON_LD_METADATA_PROVIDER =
makeMetadataManagerProviderFromSetterFactory(
JSON_LD_METADATA_SETTER_FACTORY,
{
d: [_HEAD_ELEMENT_UPSERT_OR_REMOVE, DOCUMENT],
jP: [KEY],
},
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { makeStandardMetadataProvider } from '../utils/make-standard-metadata-provider'
import {
_GLOBAL_CANONICAL_URL,
_HEAD_ELEMENT_UPSERT_OR_REMOVE,
_HeadElementUpsertOrRemove,
_injectHeadElementUpsertOrRemove,
_isDefined,
_maybeNonHttpUrlDevMessage,
_URL_RESOLVER,
Expand All @@ -12,17 +12,13 @@ import {
import { DOCUMENT } from '@angular/common'
import { Standard } from '../types'
import { MODULE_NAME } from '../module-name'
import { inject } from '@angular/core'

export const STANDARD_CANONICAL_URL_SETTER_FACTORY: (
headElementUpsertOrRemove: _HeadElementUpsertOrRemove,
doc: Document,
urlResolver: _UrlResolver,
) => MetadataSetter<Standard[typeof _GLOBAL_CANONICAL_URL]> = () => {
const headElementUpsertOrRemove = _injectHeadElementUpsertOrRemove()
const doc = inject(DOCUMENT)
const urlResolver = inject(_URL_RESOLVER)
return (url) => {
) => MetadataSetter<Standard[typeof _GLOBAL_CANONICAL_URL]> =
(headElementUpsertOrRemove, doc, urlResolver) => (url) => {
const resolvedUrl = urlResolver(url)
ngDevMode &&
_maybeNonHttpUrlDevMessage(resolvedUrl, {
Expand All @@ -40,7 +36,6 @@ export const STANDARD_CANONICAL_URL_SETTER_FACTORY: (
}
headElementUpsertOrRemove(SELECTOR, linkElement)
}
}

/**
* Manages the {@link Standard.canonicalUrl} metadata
Expand All @@ -50,6 +45,7 @@ export const STANDARD_CANONICAL_URL_METADATA_PROVIDER =
makeStandardMetadataProvider(_GLOBAL_CANONICAL_URL, {
g: _GLOBAL_CANONICAL_URL,
s: STANDARD_CANONICAL_URL_SETTER_FACTORY,
d: [_HEAD_ELEMENT_UPSERT_OR_REMOVE, DOCUMENT, _URL_RESOLVER],
})

const LINK_TAG = 'link'
Expand Down

0 comments on commit 73f64b4

Please sign in to comment.